python - 尝试从 python 中的网站下载 .csv
问题描述
我正在尝试从以下网站下载 .csv:https ://www.nasdaq.com/market-activity/stocks/screener?exchange=nyse&letter=0&render=download
任何帮助,将不胜感激。
解决方案
正如@JoeAkanesuvan 所指出的,信息是通过 JSON API 获得的。这可以使用requests
库进行访问。然后可以使用 Python 将其转换为 CSV 文件,如下所示:
import requests
import csv
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0",
}
url = "https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=3296&exchange=nyse"
r = requests.get(url, headers=headers)
j = r.json()
table = j['data']['table']
table_headers = table['headers']
with open('Stocks.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=table_headers.values(), extrasaction='ignore')
csv_output.writeheader()
for table_row in table['rows']:
csv_row = {table_headers.get(key, None) : value for key, value in table_row.items()}
csv_output.writerow(csv_row)
我建议您print(j)
更好地了解返回的数据的结构。
这会给你输出开始:
Symbol,Name,Last Sale,Net Change,% Change,Market Cap
BABA,Alibaba Group Holding Limited American Depositary Shares each representing eight Ordinary share,$260.25,-5.67,-2.132%,"704,141,925,150"
TSM,Taiwan Semiconductor Manufacturing Company Ltd.,$121.74,-4.91,-3.877%,"631,343,640,000"
JNJ,Johnson & Johnson Common Stock,$167.88,-2.60,-1.525%,"441,951,263,775"
推荐阅读
- visual-studio-code - 如何在不更改设置的情况下在 Visual Studio Code 中搜索 node_modules?
- go - go 支持多类型吗?(泛型类型)
- reactjs - 这两个 if-else 块和条件运算符块有什么区别?
- java - 顺便说一句,私人课程与 set 和 get 以及公共课程的区别
- angular - How to update index in from array cons body{} Angular 8
- python - 图形未显示在 conda 下的 Jupyter 全新安装和使用 viewscad 上游示例
- amazon-web-services - AWS 禁止 403
- kubernetes - 如何使用 manifest yaml 在 Kubernetes configmap 中挂载属性文件
- html - How to get to display database items in ejs/html using Nodejs/mongodb/mongoose?
- php - How to insert multiple rows into a table at one time?