如何使用Python从网站下载CSV文件

t8e9dugd  于 2023-07-31  发布在  Python
关注(0)|答案(1)|浏览(176)

我使用Python连接到网站https://www.nasdaq.com/market-activity/stocks/screener,并按下下载CSV按钮以获取所有纳斯达克股票的列表。
我使用了下面的代码,但是当我运行它的时候什么也没有发生。你能给我提供一些指导来纠正我的代码吗?我不清楚如何在谷歌浏览器上看到如何找到Python下载csv文件并正确编码csv_url_extension?它似乎没有按钮“下载CSV”在谷歌浏览器的开发?您能给我提供一些指导来纠正下面的代码吗:

import requests
from bs4 import BeautifulSoup

# url of initial page with data
url = "https://www.nasdaq.com/market-activity/stocks/screener"
# name of csv file where to store downloaded csv data
csv_file_name = "/Users/document/tickers_nasdaq.csv"

# get html content of initial page
html_data = requests.get(url=url) \
                .content
# generate Beautifulsoup object based on hrml content of initial page
soup = BeautifulSoup(markup=html_data)
# extract url extension of downloadable csv file
csv_url_extension = soup.find(attrs={'class': "path-node page-node- 
type-market-activity-page with-header-ads with-header-ads--loaded"}) \
                    .get(key='href')
# construct url of downloadable csv file
csv_url = "https://www.nasdaq.com/market-activity/stocks/screener" + csv_url_extension
# get content of downloadable csv file and saving it to file
response = requests.get(url=csv_url)
if response.status_code == 200:
   with open(csv_file_name, 'wb') as file:
        file.write(response.content)

字符串

klr1opcd

klr1opcd1#

以下是方法:

import csv

import requests

csv_url = "https://api.nasdaq.com/api/screener/stocks?tableonly=true&limit=25&offset=0&download=true"
headers = {
    "Accept": "application/json, text/plain, */*",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67",
}
data = requests.get(csv_url, headers=headers).json()['data']

columns = data['headers']
body = data['rows']

with open('stocks.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(columns)
    for item in body:
        writer.writerow(item.values())

字符串
输出量:
.csv文件如下所示:


的数据

相关问题