繁体   English   中英

如果下载按钮没有 URL,如何从网站下载 CSV 文件?

[英]How to download a CSV file from a website if the download button does not have a URL?

这个网站有“下载 CSV”按钮,但它不会指向特定的 URL,我只能从 Python 或 ZE1E1D3D40573127E9ZEE0480CAF1283D6 程序内部调用。 有没有办法自主完成?

通常,在 Python 中,我会这样做:

import pandas as pd
data = pd.read_csv('csv_url')

但是由于纳斯达克网站缺少 URL,我该如何实现呢?

Looking around in the code on that website, it looks like the site gets data from JSON that it requests from the url https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true

由于它可能不需要是 CSV,因此您可以使用它。

data = pd.read_json("https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true")

从测试来看,URL 的唯一重要参数似乎是download=true ,因此可以简化为https://api.nasdaq.com/api/screener/stocks?download=true

让我告诉你我是怎么做到的,然后我会告诉你一个更好的方法来做到这一点。

这些按钮不像您习惯的那样是<href> 它正在调用一些 javascript 来拉动 CSV。

使用 chrome,我打开检查器 (Ctrl+I),然后切换到网络选项卡。 我点击了按钮,只是寻找传入的 csv。 这给了我 url,您可以在其中下载数据。

 https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true

出于某种原因,它返回的是 json,而不是 csv,但您仍然可以使用

  pd.read_json(https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true)

现在这是使用本网站的正确方法。 纳斯达克有一个 API记录在这里 使用他们的 API 将为您提供更可靠和稳定的数据连接,而无需求助于使用 Chrome 的检查器之类的技巧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM