I am trying to download a .csv from the following website: https://www.nasdaq.com/market-activity/stocks/screener?exchange=nyse&letter=0&render=download
Any help would be appreciated.
I am trying to download a .csv from the following website: https://www.nasdaq.com/market-activity/stocks/screener?exchange=nyse&letter=0&render=download
Any help would be appreciated.
As @JoeAkanesuvan has pointed out, the information is obtained via a JSON API. This can be accessed using the requests
library. It can then be converted into a CSV file using Python as follows:
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)
I would recommend you print(j)
to better understand the structure of the data being returned.
This would give you output starting:
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"