Home > other >  About the try... Except, and time. The sleep ()
About the try... Except, and time. The sleep ()

Time:11-20

I am a novice, in order to learn the crawler I saw several bosses, and learned a try... Except,
I think this is a used for error feedback function, but I found in the use of time. The sleep () can also stop the try the code inside and then turn to run the except the code inside,
Seem to try the following code to run to time. The sleep (when the will is considered to be an error, and then call the except the code below, or should I say, as long as the try the code inside stop runtime will stop the subsequent code, running the except the code inside,
Don't know that I understand right,
-- -- -- -- -- -- -- -- -- --
This is a part of the crawlers
The two added starred time. Sleep (1)
The first tip can lead to keep [hero] folder has been created, but will not affect the program runs,
The second will cause the program again after printing a server status code print application runtime error occurs then stop running,
-- -- -- -- -- -- -- -- -- -- --
I=0
# try to create the folder if an error prompt
Try:
Try:
OS. The mkdir (F: \ \ "" king")
Except:
Print (' folder of Kings test existing ')
The variable J # in heronumber cycle again
For j in heronumber:
Try:
OS. The mkdir (" F: \ \ \ \ "king test + heroname [I])
A star * time. Sleep (1)
Except:
Print (' folder '+ STR (heroname [I]) +' existing ')
OS. The chdir (" F: \ \ \ \ "king test + heroname [I])
I +=1
For k in range (10) : # 10 k cycle time to download all skin
Onehero_link='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + STR (j) + "/" + STR (j) + '- bigskin -' + STR (k) + 'JPG'
Im=requests. Get (onehero_link)
Try:
If im. Status_code==200:
Open (STR (k) + 'JPG', 'wb), write (im) content)
Print (STR (im) status_code))
2 star * time. Sleep (1)
Except:
Print (' feedback server code + STR (im) status_code))
Except:
Print (' application runtime error occurs)

CodePudding user response:

The first star time. Sleep in a try, will perform the except something goes wrong, don't stop, cycle time, hint at a time, until the end of the cycle, no problem!
The second star time. Sleep in on another try, error is executed except, also no problem,
What do you want to ask what
Wrong is not the time, but
OS. The mkdir (F: \ \ "" king")
And
Open (STR (k) + 'JPG', 'wb), write (im) content) (this error is I guess)

You reorganize your language

CodePudding user response:

The building Lord, you have misunderstanding on the exception handling ha!
Can see from your code, you don't need to create folder exception handling!
Give you a code fragment, the hope can help to you!

 
The import OS

Basedir="F: \ \ test of Kings"
If not OS. Path. The exists (basedir) :
OS. The mkdir (basedir)



If you want to create multiple folders, you can in accordance with the gourd ladle, switch to
OS. makedirs () function

CodePudding user response:

references on 1st floor day I reply:
the first star time. Sleep in a try, and will perform the except something goes wrong, don't stop, cycle time, hint at a time, until the end of the cycle, no problem!
The second star time. Sleep in on another try, error is executed except, also no problem,
What do you want to ask what
Wrong is not the time, but
OS. The mkdir (F: \ \ "" king")
And
Open (STR (k) + 'JPG', 'wb), write (im) content) (this error is I guess)

You reorganize your language

Thank you I will see
  • Related