Basic python and python tutorials
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.
In python, only the same type can be connected with "+", so the code in the previous article can be changed
roomUrl = 'http://live.bilibili.com/'+ roomId
Now we have expanded our knowledge to solve the problem. The powerful python has a string processing method. Now we only need to judge whether the entered character is a number. Call the isdigit () function to complete the requirement. I will sort out other string processing methods and post them to my blog.
Isdigit () meaning: If the string contains only numbers, True is returned; otherwise, False is returned.
After the judgment, the program will end. This is not what we want. What we need is an infinite judgment. If it is not a number, we need to return and re-enter it. If it is a number, we need to proceed to the next step.
Ideas:
1. Enter the room number.
2. Check whether the entered number is correct.
3. If it is a number, proceed to the next step; if it is not a number, re-enter it.
4. Judge after the input is complete, and then loop through it.
Code:
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)
:
Check that the room number is empty and needs to be re-entered. The numbers and letters must be re-entered to meet our needs. We will continue to solve the next bug.
The entered room does not exist (that is, the website does not exist ):
This is a part of program exception handling. We only need to find out the cause of the program error.
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 ))
: