我試圖使用Python登錄到一個網站,並從多個網頁中收集信息,我得到以下錯誤:如何避免HTTP錯誤429(太多請求)蟒蛇
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")
有沒有辦法解決它,這是對服務器 - 執法側面跟蹤您製作多少個請求/時間單位。如果你超過這個單位,你會被暫時封鎖。有些服務器在標題中發送這些信息,但這些情況很少見。 檢查從服務器收到的標題,使用可用的信息..如果不是,請檢查您能夠多快地敲打而不會被抓到並使用「睡眠」。 – Torxed
http://stackoverflow.com/questions/15648272/how-do-you-view-the-request-headers-that-mechanize-is-using – Torxed