当前位置: 首页 > 知识库问答 >
问题:

请帮我破解这个错误消息?-python

华衡
2023-03-14

我用python编写了以下代码,该代码转到数组中的URL并查找有关该页面的特定信息 - 各种Web抓取器。这个接受一个Reddit线程数组,并输出每个线程的分数。该程序几乎从不完全执行。通常,在收到下面的错误消息之前,我会经历5次左右的迭代。有人可以帮我了解这一点吗?

import urllib2
from bs4 import BeautifulSoup

urls = ['http://www.reddit.com/r/videos/comments/1i12o2/soap_precursor_to_a_lot_of_other_hilarious_shows/', 'http://www.reddit.com/r/videos/comments/1i12nx/kid_reporter_interviews_ryan_reynolds/', 'http://www.reddit.com/r/videos/comments/1i12ml/just_my_two_boys_going_full_derp_shocking_plot/']

for x in urls:
    f = urllib2.urlopen(x)
    data = f.read()
    soup = BeautifulSoup(data)
    span = soup.find('span', attrs={'class':'number'})
    print '{}:{}'.format(x, span.text)

我收到的错误消息是:

Traceback (most recent call last):
  File "C:/Users/jlazarus/Documents/YouTubeparse2.py", line 7, in <module>
    f = urllib2.urlopen(x)
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 429: Unknown

共有1个答案

阮星火
2023-03-14

使用try-and-except规则忽略以捕获错误,如果您只想跳过错误,这就是您想要的。

import urllib2
from bs4 import BeautifulSoup

urls = ['http://www.reddit.com/r/videos/comments/1i12o2/soap_precursor_to_a_lot_of_other_hilarious_shows/', 'http://www.reddit.com/r/videos/comments/1i12nx/kid_reporter_interviews_ryan_reynolds/', 'http://www.reddit.com/r/videos/comments/1i12ml/just_my_two_boys_going_full_derp_shocking_plot/']

for x in urls:
    try:
        f = urllib2.urlopen(x)
        data = f.read()
        soup = BeautifulSoup(data)
        span = soup.find('span', attrs={'class':'number'})
        print '{}:{}'.format(x, span.text)
    except HTTPError:
        print("HTTP Error, continuing")
 类似资料:
  • 错误 第6:44行:在函数“app”中调用React Hook“useState”,该函数既不是React函数组件,也不是自定义React Hook函数React Hook/rules of Hook 搜索关键字以了解有关每个错误的更多信息。

  • 第1034行:Char 9:运行时错误:引用绑定到'std::vector'类型的空指针

  • 错误: log4j:WARN找不到记录器(org.hibernate.cfg.annotations.Version)的appender。log4j:警告请正确初始化log4j系统。线程“main”组织中出现异常。冬眠例外GenericJDBCException:无法在组织上打开连接。冬眠例外SQLStateConverter。在org上处理非特定异常(sqlstatecoverter.java:

  • 问题内容: 以下段错误消息的正确解释是什么? 问题答案: 这是一个段错误,原因是跟随空指针试图查找要运行的代码(即在指令提取期间)。 如果这是一个程序,而不是共享库 运行(并重复给定的其他指令指针值)以查看错误发生的位置。更好的方法是,获得一个带有调试工具的内部版本,并在诸如gdb之类的调试器下重现该问题。 由于是共享库 不幸的是,您被水淹了。事后无法知道动态链接程序将库放置在内存中的位置。重现该

  • 我想在一个controller方法中创建一个新的Role对象,然后该对象显示一个链接到Hibernate/H2数据库的数据存储库中的所有“角色”,但每次我尝试创建一个新对象时,都会出现一个SQL错误,对我来说这似乎不对。如果有人能帮忙,那就太好了。 下面是repo-https://github.com/danielturato/instateam-th 对于角色实体,我尝试了以下操作: 将名称字段

  • 我使用下面的代码发出http请求,但当抛出异常时,我无法获取每个字段的错误消息。在postman中,它确实显示了正确的错误消息: 它只是显示消息“” 然而,在《邮递员》中,它显示: