这是链接 - http://armstrade.sipri.org/armstrade/page/values.php
我正在尝试编写一个Python代码,以便在下拉菜单中为每个状态自动下载这些CSV文件。我的代码如下:
submit_value={'country_code':'ALG','low_year':'2010','high_year':'2018','import_or_export':'import','summarize':'country','filetype':'html','Action':'Download'}
page = requests.post(url='http://armstrade.sipri.org/armstrade/page/values.php',data=submit_value)
但是,这并没有给我任何新的东西。相反,我在page.text中的内容与原始HTML页面完全相同。这表明所有这些参数(年份范围,国家/地区代码等)都未提交到页面。
知道我怎么能这样做?非常感激!!
你已经快到了。检查请求和响应(例如,在Chrome工具的“网络”标签中)
submit_value={'country_code':'ALG','low_year':'2010','high_year':'2018','import_or_export':'import','summarize':'country','filetype':'html','Action':'Download'}
response = requests.post(url='http://armstrade.sipri.org/armstrade/html/export_values.php',data=submit_value)
with open("/tmp/sample.hmtl", "w") as f:
f.write(response.text)
工作得很好!
UPD :(由于我的答案中的拼写错误而决定突出显示)
response.text
包含数据(也可能是response.content
,只有字节)尝试使用GET请求而不是POST请求,因为您没有输入任何值,而是从客户端请求一些数据。
你的脚本工作得非常好。 page.content保存所有响应数据。谢谢。
import requests
submit_value={'country_code':'ALG','low_year':'2010','high_year':'2018','import_or_export':'import','summarize':'country','filetype':'html','Action':'Download'}
page = requests.post(url='http://armstrade.sipri.org/armstrade/page/values.php',data=submit_value)
print(page.content)