Python crawls stock data, you must have data for quantitative trading!

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join

Basic environment configuration

  • python 3.6
  • pycharm
  • requests
  • csv
  • time

The relevant module pip can be installed

Landing page

 

Analyze web pages

Everything is in the picture


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

Request page

import requests
url = 'https://xueqiu.com/service/v5/stock/screener/quote/list'
response = requests.get(url=url, params=params, headers=headers, cookies=cookies)
html_data = response.json()
1234

Analytical data

data_list = html_data['data']['list'] 
for i in data_list: 
    dit = {} 
    dit['stock code'] = i['symbol'] 
    dit['stock name'] = i['name'] 
    dit['current price'] = i['current'] 
    dit['change amount'] = i['chg'] 
    dit['change/%'] = i['percent'] 
    dit['beginning of the year So far/%'] = i['current_year_percent'] 
    dit['volume'] = i['volume'] 
    dit['volume'] = i['amount'] 
    dit['turnover rate/%'] = i['turnover_rate'] 
    dit['P/E ratio TTM'] = i['pe_ttm'] 
    dit['dividend rate/%'] = i['dividend_yield'] 
    dit['market value'] = i['market_capital'] 
    print (dit) 
12345678910111213141516

save data

import csv 
f = open('stock data.csv', mode='a', encoding='utf-8-sig', newline='') 
csv_writer = csv.DictWriter(f, fieldnames=['stock code', 'Stock name','current price','volume of change','volume of change/%','year to date/%','volume','volume','turnover rate/%', ' P/E ratio TTM','Dividend rate/%','Market value']) 
csv_writer.writeheader() 
csv_writer.writerow(dit) 
f.close() 
123456

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/109059727