抓一个需要提交表单的php网页

问题描述 投票:1回答:3

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

知道我怎么能这样做?非常感激!!

python web-scraping request
3个回答
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,只有字节)

0
投票

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


-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)
© www.soinside.com 2019 - 2024. All rights reserved.