我正在尝试从这样的在线计算器中获取数据:
https://srv111.services.gc.ca/INT_02.aspx
它需要在多个页面中填写表格。
我正在尝试使用 python 请求库来执行此操作,但没有运气。据我了解,
import requests
s = requests.Session()
然后我浏览每一页
s.get(url1)
r2 = s.post(url2, data=payload) #payload is form data created by looking at page source
...
但这似乎不起作用。当我
print r2.text
我应该得到下一页,但我得到的是同一页。我对这些东西很陌生。有人可以指出我的错误。非常感谢!
编辑:这是代码:
import requests
import codecs
url1='https://srv111.services.gc.ca/INT_01.aspx?lang=e'
url2='https://srv111.services.gc.ca/INT_02.aspx'
url3='https://srv111.services.gc.ca/OAS_01.aspx'
url4='https://srv111.services.gc.ca/OAS65_01.aspx'
url5='https://srv111.services.gc.ca/OAS_11.aspx'
payload2={'ctl00$ContentPlaceHolder1$ddlMonth':'1', 'ctl00$ContentPlaceHolder1$txtYear':'1987', 'ctl00$ContentPlaceHolder1$lbGender':'1', 'ctl00$ContentPlaceHolder1$btnNext': 'Next'}
s = requests.Session()
r1 = s.get(url1)
c = {'ASP.NET_SessionId': r1.cookies['ASP.NET_SessionId']}
r2 = s.post(url2, data=payload2, cookies=c)
payload3 = {'ctl00$ContentPlaceHolder1$btnNextPrevious2$btnNext':'Next'}
r3 = s.post(url3, data=payload3, cookies=c)
payload4 = {'ctl00$ContentPlaceHolder1$lbYesNo':'-1'}
r4 = s.post(url4, data=payload4)
r5 = s.get(url5)
with codecs.open('result.html','w','utf-8') as f:
f.write(r2.text)