1

这是链接 - 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 页面完全相同。这表明所有这些参数(年份范围、国家代码等)都没有提交到页面。

知道我怎么能做到这一点?非常感激!!

4

3 回答 3

0

尝试使用 GET 请求而不是 POST 请求,因为您没有输入任何值,而是从客户端请求一些数据。

于 2019-03-27T21:50:15.613 回答
0

你已经快到了。检查请求和响应(例如,在 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:( 由于我的回答中的错字而决定突出显示)

  1. 它是html/export_values .php,请求应该去那里
  2. response.text包含数据(也可以response.content,只是那里的字节)
于 2019-03-27T22:13:57.053 回答
-1

你的脚本工作得很好。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)

于 2019-03-27T22:04:01.253 回答