Python Mechanize 阻止连接 :Close

标签 python mechanize

我正在尝试使用 mechanize 从网页获取信息。它基本上成功地获取了第一部分信息,但网页包含一个“下一步”按钮以获取更多信息。我不知道如何以编程方式获取附加信息。

通过使用实时 HTTP header ,我可以看到在浏览器中单击“下一步”按钮时生成的 http 请求。似乎我可以使用 mechanize 发出相同的请求,但在后一种情况下,我没有获得下一页,而是被重定向到网站的主页。

显然,mechanize 正在做一些与我的浏览器不同的事情,但我不知道是什么。在比较标题时,我确实发现了一个区别,那就是使用的浏览器

连接:保持事件状态

使用 Mechanize

连接:关闭

我不知道这是否是罪魁祸首,但是当我尝试添加 header (“Connection”、“keep-alive”)时,它没有改变任何内容。

[更新] 当我在 Firefox 中单击“第 2 页”按钮时,生成的 http 是(根据实时 HTTP header ):

GET /statistics/movies/ww_load/the-fast-and-the-furious-6-2012?authenticity_token=ItU38334Qxh%2FRUW%2BhKoWk2qsPLwYKDfiNRoSuifo4ns%3D&facebook_fans_page=2&tbl=facebook_fans&authenticity_token=ItU38334Qxh%2FRUW%2BhKoWk2qsPLwYKDfiNRoSuifo4ns%3D HTTP/1.1
Host: www.boxoffice.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
X-Prototype-Version: 1.6.0.3
Referer: http://www.boxoffice.com/statistics/movies/the-fast-and-the-furious-6-2012
Cookie: __utma=179025207.1680379428.1359475480.1360001752.1360005948.13; __utmz=179025207.1359475480.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __qca=P0-668235205-1359475480409; zip=13421; country_code=US; _boxoffice_session=2202c6a47fc5eb92cd0ba57ef6fbd2c8; __utmc=179025207; user_credentials=d3adbc6ecf16c038fcbff11779ad16f528db8ebd470befeba69c38b8a107c38e9003c7977e32c28bfe3955909ddbf4034b9cc396dac4615a719eb47f49cc9eac%3A%3A15212; __utmb=179025207.2.10.1360005948
Connection: keep-alive

当我尝试在 mechanize 中请求相同的 url 时,它看起来像这样:

GET /statistics/movies/ww_load/the-fast-and-the-furious-6-2012?facebook_fans_page=2&tbl=facebook_fans&authenticity_token=ZYcZzBHD3JPlupj%2F%2FYf4dQ42Kx9ZBW1gDCBuJ0xX8X4%3D HTTP/1.1
Accept-Encoding: identity
Host: www.boxoffice.com
Accept: text/javascript, text/html, application/xml, text/xml, */*
Keep-Alive: 115
Connection: close
Cookie: _boxoffice_session=ced53a0ca10caa9757fd56cd89f9983e; country_code=US; zip=13421; user_credentials=d3adbc6ecf16c038fcbff11779ad16f528db8ebd470befeba69c38b8a107c38e9003c7977e32c28bfe3955909ddbf4034b9cc396dac4615a719eb47f49cc9eac%3A%3A15212
Referer: http://www.boxoffice.com/statistics/movies/the-fast-and-the-furious-6-2012
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1

-- 达里尔

最佳答案

服务器正在检查X-Requested-With和/或X-Prototype-Version,因此将这两个 header 添加到 Mechanize 请求中可以修复它。

关于Python Mechanize 阻止连接 :Close,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693836/

相关文章:

python - Python 中的变量 : Add and change variables

python - 获得 Mechanize 和斜纹布说话

python - tkinter 和 Tkinter 之间的区别

Python赋值并在同一行使用

Python Mechanize,单击 <li> 链接?

javascript - 以与 Google bot 类似的方式抓取网站 html 和 javascript

python - 无法使用 Mechanize 找到表格

selenium - 单击 aspnet 链接 - 不是表单 - 使用 selenium

python - PySpark 中等效的 Scala 案例类是什么?

python - 值错误: Input array dimensions not right for CountVectorizer()