Home >Backend Development >Python Tutorial >Detailed explanation of python-bilibili error solution

Detailed explanation of python-bilibili error solution

高洛峰
高洛峰Original
2017-03-27 15:46:323559browse

Before getting the room number, we first solve the bug left in the previous article, that is, the problem caused by the entered room number not being a number and the corresponding room number not existing.

The room number entered is not a number:

In python, what you enter must be a string. Although you enter a number, the type is still str.


roomId = input('请输入房间号:')

Using the code from the previous article, let’s test it

详解python - bilibili出错的解决办法

In python, only the same type can be connected with "+", so the code in our previous article can also be changed to


roomUrl = 'http://live.bilibili.com/'+ roomId

. Now that we have expanded our knowledge, let’s solve the problem now. There are string processing methods in the powerful Python. Now we only need to determine whether the entered characters are numbers. Calling the isdigit() function can complete the requirement. There are other string processing methods that I will sort out and post on the blog.

isdigit() Meaning: Returns True if the string contains only digits, otherwise returns False.

The program ends after the judgment is completed. This is not what we want. What we need is infinite judgment. If it is not a number, we have to go back and re-enter. If it is a number, we need to proceed to the next step.

Ideas:

1. Enter the room number.

2. Determine whether the input is a number.

3. If it is a number, proceed to the next step; if it is not a number, re-enter it.

4. After the input is completed, make a judgment and start the cycle.

Code:


roomId = input('请输入房间号:')while not roomId.isdigit():     
print("数字格式错误,请重新输入!")
     roomId = input('请输入房间号:')
roomUrl = 'http://live.bilibili.com/'+ str(roomId)

Rendering:

详解python - bilibili出错的解决办法

Looking at the rendering, the room number is empty It also needs to be re-entered, and the form of numbers plus letters also needs to be re-entered to meet our needs. Let's continue to solve the next bug.

The entered room does not exist (that is, the URL does not exist):

This is for program exception handling. We only need to find out the cause of the program's error.

详解python - bilibili出错的解决办法

Focus on the last sentence, urllib.error.HTTPError: HTTP Error 404: Not Found

Webpage 404, no webpage found. It means that the web address you entered does not exist. Use try-except to resolve this error.

Ideas:

1. When accessing a URL, you don’t know whether it is there or not

2. Try to access this address

3. If you make an error, you need Re-enter the URL

4. To re-enter the URL, you need to start by entering the room number

5. The big loop is when the URL is wrong at the beginning, and the small loop is to determine the room number

6 , the entered URL exists, then you need to jump out of this big loop

Code:

while True:
     roomId = input('请输入房间号:')
     while not roomId.isdigit():
          print("数字格式错误,请重新输入!")
          roomId = input('请输入房间号:')
     roomUrl = 'http://live.bilibili.com/'+ str(roomId)

     try:
          webPage=urllib.request.urlopen(roomUrl)
          break
     except:
          print('出错啦!')

However, the user experience is not very good, and we don’t know what went wrong, so we bring the specific Reason


except urllib.error.HTTPError as reason:          
print('网址出错啦!'+ str(reason))

Rendering:

详解python - bilibili出错的解决办法

##

The above is the detailed content of Detailed explanation of python-bilibili error solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn