Get the App
SLTechnology News&Howtos  ›  Development  › 

How to collect stock data and make visual bar chart by Python

Shulou Source: shulou.com Published: 2022-06-01 01:21:29 09月23日 Update

The knowledge of this article "Python how to collect stock data and make visual bar chart" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python how to collect stock data and make visual bar chart" article.

Module use

Requests > pip install requests (data request third party module)

Re # regular expression to match the extracted data

Json

Pandas

Pyecharts

Development environment

Python 3.8interpreter

Pycharm version 2021.2

Code implementation steps

Send a request to visit the website

Get data

Parsing data (extracting data)

Save data

Make a simple visualization of the bar chart

Code # 1. Send a request to visit the website headers= {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'} url= 'https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1641730868838'response = requests.get (url=url, headers=headers) # 2. Get data json_data = response.json () # 3. Data parsing (filtering data) data_list = json_data ['data'] [' list'] for data in data_list: data1 = data ['symbol'] data2 = data [' name'] data3 = data ['current'] data4 = data [' chg'] data5 = data ['percent'] data6 = data [' current_year_percent'] data7 = data ['volume'] data8 = data [' amount'] Data9 = data ['turnover_rate'] data10 = data [' pe_ttm'] data11 = data ['dividend_yield'] data12 = data [' market_capital'] print (data1 Data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12) data_dict = {'stock symbol': data1, 'stock name': data2, 'current price': data3,'up and down': data4,'up and down': data5, 'year to date': data6, 'volume': data7, 'turnover': data8 'turnover': data9, 'TTM': data10, 'dividend yield': data11, 'market capitalization': data12,} csv_write.writerow (data_dict) 4. Save address file = open ('data2.csv', mode='a', encoding='utf-8', newline='') csv_write = csv.DictWriter (file, fieldnames= [' stock symbol', 'stock name', 'current price','up and down','up and down', 'year to date', 'volume', 'turnover', 'turnover', 'price-to-earnings ratio (TTM)', 'dividend yield', 'market capitalization']) csv_write.writeheader ()

Running effect

Data visualization data_df = pd.read_csv ('data2.csv') df = data_df.dropna () df1 = df [[' stock name'' Df2 = df1.iloc [: 20] print (df2 ['stock name'] .values) print (df2 ['stock name'] .values) c = (Bar () .add _ xaxis (df2 ['stock name'] .values.tolist ()) .add _ yaxis ("stock trading volume") Df2 ['Trading Volume'] .values.tolist () .set _ global_opts (title_opts=opts.TitleOpts (title= "Trading Volume Chart-Volume chart"), datazoom_opts=opts.DataZoomOpts (),) .render ("data.html") print ('data visualization result is complete, please find the open data.html file in the current directory!')

The above is about the content of this article on "how Python collects stock data and makes visual bar charts". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Data stocks trading volume visualization content name column code production market capitalization price earnings price / earnings ratio turnover articles modules steps ups and downs knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Microsoft Shulou Information Docker