Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Python to crawl stock data

Shulou Source: shulou.com Published: 2022-06-02 01:14:22 09月29日 Update

Today, I will talk to you about how to use Python to crawl stock data, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Basic environment configuration

Python 3.6

Pycharm

Requests

Csv

Time

The related module pip can be installed.

Target web page

Find the data, directly request the web page, parse the data, save the data

Request web page import requestsurl = 'https://xueqiu.com/service/v5/stock/screener/quote/list'response = requests.get (url=url, params=params, headers=headers) Cookies=cookies) html_data = response.json () parsing data data_list = html_data ['data'] [' list'] for i in data_list: dit = {} dit ['stock symbol'] = I ['symbol'] dit [' stock name'] = I ['name'] dit [' current price'] = I ['current'] dit [' up and down'] = I ['chg'] dit [' /%'] = I ['percent'] dit [' year to date /%'] = I ['current_year_percent'] dit [' turnover'] = I ['volume'] dit [' turnover'] = I ['amount'] dit [' turnover /%'] = I ['turnover_rate'] dit [' price-to-earnings ratio TTM'] = I ['pe_ttm'] dit [' dividend Rate /%'] = I ['dividend_yield'] dit [' market capitalization'] = I ['market_capital'] print (dit)

Save data import csvf = open ('stock data .csv', mode='a', encoding='utf-8-sig', newline='') csv_writer = csv.DictWriter (f, fieldnames= ['stock symbol', 'stock name', 'current price','up and down amount','up and down /%', 'year to date /%', 'volume', 'turnover', 'turnover /%','P / E TTM'' 'dividend yield /%', 'market capitalization'] csv_writer.writeheader () csv_writer.writerow (dit) f.close () read the above content Do you know more about how to crawl stock data using Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Data stocks content web pages codes names market capitalization price earnings price-earnings ratio trading volume turnover ups and downs dividends turnover more modules environment goals knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL MariaDB Shulou Technology Docker