Before getting the room number, we should first solve the bug left over from the previous article, that is, the entered room number is not a problem because the number does not exist and the corresponding room number does not exist. Before getting the room number, we should first solve the bug left over from the previous article, that is, the entered room number is not a problem because the number does not exist and the corresponding room number does not exist.
The entered room number is not a number:
In python, what you enter must be a string. although you enter a number, the type is str.
RoomId = input ('Enter the room number :')
Using the code in the previous article, let's test it.
RoomId = input ('Enter the room number: ') while not roomId. isdigit (): print ("The number format is incorrect. please enter it again! ") RoomId = input ('Enter the room number: ') roomUrl = 'http: // live.bilibili.com/'+ str (roomId)
:
In the last sentence, urllib. error. HTTPError: HTTP Error 404: Not Found.
Webpage 404, no webpage found. The webpage address you entered does not exist. Try-try T to solve this error.
Ideas:
1. accessing a website does not know whether it is in
2. try to access this address.
3. if an error occurs, enter the URL again.
4. re-enter the website address and start from entering the room number.
5. a large cycle starts with an error on the website. a small cycle is used to determine the room number.
6. if the entered URL exists, you need to jump out of this cycle.
Code:
While True: roomId = input ('Enter the room number: ') while not roomId. isdigit (): print ("The number format is incorrect. please enter it again! ") RoomId = input ('Enter the room number: ') roomUrl = 'http: // live.bilibili.com/'+ str (roomId) try: webPage = urllib. request. urlopen (roomUrl) break failed t: print ('error! ')
However, in this way, the user experience is not good, and we do not know where the error is, so we take the specific reason
Failed T urllib. error. HTTPError as reason: print ('url error! '+ Str (reason ))
:
The above is a detailed explanation of the solution to the python-bilibili error. For more information, see other related articles in the first PHP community!