Set Mechanize to accept cookies

我只是一个虾纸丫 提交于 2019-12-24 05:16:26

问题


I am trying to use mechanize to retrieve some data from a website to automatize searching for a flat (no spamming)

However, when I send a request to the site, the response has the following headers:

header: Cache-Control: no-store, no-cache, max-age=0, must-revalidate, private,  max-     stale=0, post-check=0, pre-check=0
header: Content-Type: text/html
header: P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
header: Date: Mon, 16 Jun 2014 19:13:23 GMT
header: Connection: close
header: Set-Cookie: SPSI=1f5cf9461ca8ab1ee7f4d427ce1c895b ; path=/
header: Content-Length: 10965

and the response text says something along the lines "you must enable cookies" How can I simulate that with mechanize? Doing this doesnt seem to work:

br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

FYI, I have mechanize 0.2.5. If this doesn't work out, I am considering Selenium.


回答1:


I had the same problem. Add these browser options. From here: http://stockrt.github.io/p/emulating-a-browser-in-python-with-mechanize/

 # Browser options
 br.set_handle_equiv(True)
 br.set_handle_gzip(True)
 br.set_handle_redirect(True)
 br.set_handle_referer(True)
 br.set_handle_robots(False)

 # Follows refresh 0 but not hangs on refresh > 0
 br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

I also recommend using debug messages

 # Want debugging messages?
 #br.set_debug_http(True)
 #br.set_debug_redirects(True)
 #br.set_debug_responses(True)


来源:https://stackoverflow.com/questions/24250662/set-mechanize-to-accept-cookies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!