我使用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)
字符串
1条答案
按热度按时间klr1opcd1#
以下是方法:
字符串
输出量:
.csv
文件如下所示:的数据