我正在使用Pythong请求lib从新西兰统计网站检索数据: http://www.stats.govt.nz/infoshare/ExportDirect.aspx。 http POST请求的格式为“multipart / form-data”。我用谷歌搜索,发现请求是 一个支持多部分帖子的好图书馆。 但是,我的代码没有得到应该从服务器检索的结果。 相反,它只向我提供了网站http://www.stats.govt.nz/infoshare/ExportDirect.aspx的源代码。
有人可以帮我解决这个问题吗?
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
r = requests.get('http://www.stats.govt.nz/infoshare/ExportDirect.aspx')
soup = BeautifulSoup(r.text)
view_state = soup.find("input",id="__VIEWSTATE")["value"]
event_validation = soup.find("input",id="__EVENTVALIDATION")["value"]
post_data = {"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"__VIEWSTATE": view_state,
"__EVENTVALIDATION": event_validation,
"ctl00$MainContent$tbMissingText": "..",
"ctl00$MainContent$TimeVariableSelector$tbSelected": "1",
"ctl00$MainContent$TimeVariableSelector$lbVariableOptions": "0",
"ctl00$MainContent$rblCSVFormat": "columns",
"ctl00$MainContent$rblSeriesDescription": "no_description",
"ctl00$MainContent$fuSearchFile": (open('Alcohol.sch'), 'application/octet-stream')
}
r1 = requests.post('http://www.stats.govt.nz/infoshare/ExportDirect.aspx?AspxAutoDetectCookieSupport=1', files = post_data)
print(r1.text)
input('Enter to exit...')