【发布时间】:2014-05-12 05:42:35
【问题描述】:
我正在尝试使用 Python 登录网站并从多个网页收集信息,但出现以下错误:
Traceback (most recent call last): File "extract_test.py", line 43, in <module> response=br.open(v) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open return self._mech_open(url, data, timeout=timeout) File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open raise response mechanize._response.httperror_seek_wrapper: HTTP Error 429: Unknown Response Code
我使用了time.sleep(),它可以工作,但它似乎不智能且不可靠,有没有其他方法可以避免这个错误?
这是我的代码:
import mechanize
import cookielib
import re
first=("example.com/page1")
second=("example.com/page2")
third=("example.com/page3")
fourth=("example.com/page4")
## I have seven URL's I want to open
urls_list=[first,second,third,fourth]
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Log in credentials
br.open("example.com")
br.select_form(nr=0)
br["username"] = "username"
br["password"] = "password"
br.submit()
for url in urls_list:
br.open(url)
print re.findall("Some String")
【问题讨论】:
-
没有办法,这是服务器端的强制措施,用于跟踪您发出的请求数/时间单位。如果您超过此单位,您将被暂时阻止。一些服务器在标头中发送此信息,但这种情况很少见。检查从服务器收到的标头,使用可用的信息。如果没有,检查你可以多快敲击而不被抓住并使用
sleep。
标签: python http mechanize http-status-code-429