使用 Python Mechanize 对 aspx 进行屏幕美化 - Javascript 表单提交
我正在尝试抓取英国食品评级机构数据 aspx 搜索结果页面(例如 http:// ratings.food.gov.uk/QuickSearch.aspx?q=po30 )在 scraperwiki 上使用 Mechanize/Python ( http://scraperwiki.com/scrapers/food_standards_agency/ ),但在尝试遵循以下形式的“下一页”链接时遇到问题:
<input type="submit" name="ctl00$ContentPlaceHolder1$uxResults$uxNext" value="Next >" id="ctl00_ContentPlaceHolder1_uxResults_uxNext" title="Next >" />
表单处理程序如下所示:
<form method="post" action="QuickSearch.aspx?q=po30" onsubmit="javascript:return WebForm_OnSubmit();" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_ContentPlaceHolder1_buttonSearch')" id="aspnetForm">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
当我手动单击“下一步”链接时,HTTP 跟踪显示 __EVENTTARGET 为空?我在其他抓取工具上找到的所有婴儿床都显示了 __EVENTTARGET 的操作作为处理下一页的方式。
事实上,我不确定我想要抓取的页面如何加载下一页?无论我向抓取工具扔什么,它都只能加载第一个结果页面。 (即使能够更改每页结果的数量也会很有用,但我也不知道如何做到这一点!)
那么 - 关于如何抓取第 1+N 个结果页面 N>0 的任何想法?
I'm trying to scrape UK Food Ratings Agency data aspx seach results pages (e.,g http://ratings.food.gov.uk/QuickSearch.aspx?q=po30 ) using Mechanize/Python on scraperwiki ( http://scraperwiki.com/scrapers/food_standards_agency/ ) but coming up with a problem when trying to follow "next" page links which have the form:
<input type="submit" name="ctl00$ContentPlaceHolder1$uxResults$uxNext" value="Next >" id="ctl00_ContentPlaceHolder1_uxResults_uxNext" title="Next >" />
The form handler looks like:
<form method="post" action="QuickSearch.aspx?q=po30" onsubmit="javascript:return WebForm_OnSubmit();" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_ContentPlaceHolder1_buttonSearch')" id="aspnetForm">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
An HTTP trace when I manually click Next links shows __EVENTTARGET as empty? All the cribs I can find on other scrapers show the manipulation of __EVENTTARGET as the way of handling Next pages.
Indeed, I'm not sure how the page I want to scrape ever loads the next page? Whatever I throw at the scraper, it only ever manages to load the first results page. (Even being able to change the number of results per page would be useful, but I can't see how to do that either!)
So - any ideas on how to scrape the 1+N'th results pages for N>0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mechanize 不处理 javascript,但对于这种特殊情况,不需要它。
首先我们用 mechanize 打开结果页面
然后我们选择 aspnet 表单:
该表单有 5 个提交按钮 - 我们想要提交一个将我们带到下一个结果页面的按钮:
表单中的其他提交按钮是:
在 mechanize 中,我们可以获取这样的表单信息:
Mechanize doesn´t handle javascript, but for this particular case it isn´t needed.
First we open the result page with mechanize
Then we select the aspnet form:
The form has 5 submit buttons - we want to submit the one that takes us to the next result page:
The other submit buttons in the form are:
In mechanize we can get form info like this:
Mechanicalize 不处理 JavaScript。
然而,有很多方法可以处理这个问题,包括 QtWebKit , python-spidermonkey, HtmlUnit(使用 Jython),或 SeleniumRC。
以下是如何使用 SeleniumRC 完成此操作:
另请参阅这些相关的 SO 问题:
JavaScript
Python
Mechanize does not handle JavaScript.
There are many ways to handle this, however, including QtWebKit, python-spidermonkey, HtmlUnit (using Jython), or SeleniumRC.
Here is how it might be done with SeleniumRC:
See also these related SO questions:
JavaScript
Python