Skip to main content
 首页 » 编程设计

python之BeautifulSoup之内部服务器错误

2025年01月19日9birdshome

我正在使用 BeautifulSoup 循环访问几百个 URL。最近,一个 HTTP 错误一直困扰着我。我试图构建一个 While 循环来重复加载页面,直到加载 URL,但它仍然显示。我已经浏览了我手动循环的 URL,它们似乎没有任何问题 - 错误也不是在某个 URL 上生成的,而是看似随机的迭代。

我试图解决这个问题的代码,但没有成功:

    gameCount = 0 
    for (index, URL) in enumerate(tempLinkList): 
 
        tryCount = 0 
        while tryCount < 500: 
            tryCount += 1 
            try: 
                with urllib.request.urlopen(URL) as url: 
                    page = url.read() 
                soup = BeautifulSoup(page, "html.parser")    
            except: 
                time.sleep(0.1) 
                continue 
            else: 
                break 

非常感谢任何帮助!

请您参考如下方法:

状态码500表示

The server encountered an unexpected condition which prevented it from fulfilling the request.

这意味着,您的代码没有任何问题。问题出在网站方面。除了在一段时间后重新发送请求之外,您对此无能为力。

一般来说,状态码的百位数字都有特定的含义。

来自 Wikipedia :

  • 1xx - 信息响应
  • 2xx - 成功
  • 3xx - 重定向
  • 4xx - 客户端错误
  • 5xx - 服务器错误

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request.