我试着用ScraperWiki的工具刮一个ASP支持的网站。
我想从BBSes网站获取一个特定地区代码中的BBSmates.com列表。该网站一次显示20个BBS搜索结果,因此我将不得不做表单提交,以从一页的结果移动到下一页。
这个博客帖子帮助我开始工作。我认为下面的代码将获取314区号的BBS列表的最后一页(第79页)。
然而,我得到的回应是第一页。
url = 'http://bbsmates.com/browsebbs.aspx?BBSName=&AreaCode=314'
br = mechanize.Browser()
br.addheaders = [('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')]
response = br.open(url)
html = response.read()
br.select_form(name='aspnetForm')
br.form.set_all_readonly(False)
br['__EVENTTARGET'] = 'ctl00$ContentPlaceHolder1$GridView1'
br['__EVENTARGUMENT'] = 'Page$79'
print br.form
response2 = br.submit()
html2 = response2.read()
print html2我上面引用的博客文章提到,在他们的例子中,SubmitControl有一个问题,所以我尝试在这个表单上禁用两个SubmitControls。
br.find_control("ctl00$cmdLogin").disabled = True禁用cmdLogin生成的HTTP 500。
br.find_control("ctl00$ContentPlaceHolder1$Button1").disabled = True禁用ContentPlaceHolder1 1$Button1 1没有任何区别。提交完成了,但它返回的页面仍然是搜索结果的第1页。
值得注意的是,这个网站没有使用“页面$Next”。
有人能帮我弄清楚我需要做些什么才能让ASPX表单提交工作吗?
发布于 2012-10-29 15:45:15
您需要发布页面提供的值(EVENTVALIDATION、VIEWSTATE等)。
这段代码可以工作(请注意,它使用的是可怕的请求库,而不是机械化)
import lxml.html
import requests
starturl = 'http://bbsmates.com/browsebbs.aspx?BBSName=&AreaCode=314'
s = requests.session() # create a session object
r1 = s.get(starturl) #get page 1
html = r1.text
root = lxml.html.fromstring(html)
#pick up the javascript values
EVENTVALIDATION = root.xpath('//input[@name="__EVENTVALIDATION"]')[0].attrib['value']
#find the __EVENTVALIDATION value
VIEWSTATE = root.xpath('//input[@name="__VIEWSTATE"]')[0].attrib['value']
#find the __VIEWSTATE value
# build a dictionary to post to the site with the values we have collected. The __EVENTARGUMENT can be changed to fetch another result page (3,4,5 etc.)
payload = {'__EVENTTARGET': 'ctl00$ContentPlaceHolder1$GridView1','__EVENTARGUMENT':'Page$25','__EVENTVALIDATION':EVENTVALIDATION,'__VIEWSTATE':VIEWSTATE,'__VIEWSTATEENCRYPTED':'','ctl00$txtUsername':'','ctl00$txtPassword':'','ctl00$ContentPlaceHolder1$txtBBSName':'','ctl00$ContentPlaceHolder1$txtSysop':'','ctl00$ContentPlaceHolder1$txtSoftware':'','ctl00$ContentPlaceHolder1$txtCity':'','ctl00$ContentPlaceHolder1$txtState':'','ctl00$ContentPlaceHolder1$txtCountry':'','ctl00$ContentPlaceHolder1$txtZipCode':'','ctl00$ContentPlaceHolder1$txtAreaCode':'314','ctl00$ContentPlaceHolder1$txtPrefix':'','ctl00$ContentPlaceHolder1$txtDescription':'','ctl00$ContentPlaceHolder1$Activity':'rdoBoth','ctl00$ContentPlaceHolder1$drpRPP':'20'}
# post it
r2 = s.post(starturl, data=payload)
# our response is now page 2
print r2.text当您到达结果的末尾(结果第21页)时,您必须再次获取VIEWSTATE和EVENTVALIDATION值(每20页执行一次)。
请注意,有一些值是空的,有些值包含值。完整的清单如下:
'ctl00$txtUsername':'','ctl00$txtPassword':'','ctl00$ContentPlaceHolder1$txtBBSName':'','ctl00$ContentPlaceHolder1$txtSysop':'','ctl00$ContentPlaceHolder1$txtSoftware':'','ctl00$ContentPlaceHolder1$txtCity':'','ctl00$ContentPlaceHolder1$txtState':'','ctl00$ContentPlaceHolder1$txtCountry':'','ctl00$ContentPlaceHolder1$txtZipCode':'','ctl00$ContentPlaceHolder1$txtAreaCode':'314','ctl00$ContentPlaceHolder1$txtPrefix':'','ctl00$ContentPlaceHolder1$txtDescription':'','ctl00$ContentPlaceHolder1$Activity':'rdoBoth','ctl00$ContentPlaceHolder1$drpRPP':'20'下面讨论类似问题的Scraperwiki邮件列表:https://groups.google.com/forum/#!topic/scraperwiki/W0Xi7AxfZp0
https://stackoverflow.com/questions/13114977
复制相似问题