【问题标题】:python: How can I download data from the webpage where the link is hidden by the download button?python:如何从下载按钮隐藏链接的网页下载数据?
【发布时间】:2018-02-22 04:16:24
【问题描述】:

假设我要在这里下载数据:http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html

当点击如下所示的按钮时,我得到了一个.csv 文件:

我想使用 python 自动执行此操作,我可以在其中指定日期等。

我发现here 可以使用pandas pd.read_csv 从网页读取数据,但首先需要获取正确的url。但是在我的情况下,我不知道网址是什么。

另外,我还想自己指定日期和合同等。

在问之前,我实际上尝试了开发工具,我仍然看不到url,我不知道如何让它编程。

【问题讨论】:

  • 使用浏览器中的开发者工具获取 url。
  • javascript:exportData('excel'),你可以点击那个按钮,程序化。
  • @MrPyCharm 查看编辑。
  • @bhansa 你能具体点吗?
  • 正如@bhansa 所说,这可以通过编程方式实现。 Selenium python 绑定将为您完成。 selenium-python.readthedocs.io/getting-started.html

标签: javascript python html pandas csv


【解决方案1】:

javascript exportData('excel') 生成一个提交的表单。通过使用 Chrome devtools 和 Network 面板,你可以找出所使用的 headers 和 post 数据,然后编写一个 python 脚本来提交一个相同的 http 请求。

import requests
url = 'http://www.dce.com.cn/publicweb/quotesdata/exportMemberDealPosiQuotesData.html'
formdata = {
    'memberDealPosiQuotes.variety':'a',
    'memberDealPosiQuotes.trade_type':0,
    'contract.contract_id':'all',
    'contract.variety_id':'a',
    'exportFlag':'excel',
}
response = requests.post(url, data=formdata)
filename = response.headers.get('Content-Disposition').split('=')[-1]
with open(filename, 'wb') as fp:
    fp.write(response.content)

可能会找到修改帖子数据以获取不同数据的方法。通过逆向工程、反复试验或查找一些文档。

例如,您可以包含年份和日期字段:

    'year':2017,
    'month':3,
    'day':20

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多