preface

The text and pictures in this article come from the network, only for learning, communication, do not have any commercial purposes, if you have any questions, please contact us to deal with.

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join

Basic Environment Configuration

  • Python 3.6
  • pycharm
  • requests
  • csv
  • time

The related module PIP can be installed

Target page

! [](https://p9-tt-ipv6.byteimg.com/large/pgc-image/33f29fd336374f239422173b0fb08a9c)
! [](https://p6-tt-ipv6.byteimg.com/large/pgc-image/5f3a2fc8b430458bbf0ba6d24413329f)

Analysis of web page

Everything is in The Turi

! [](https://p1-tt-ipv6.byteimg.com/large/pgc-image/8952fe61f48847e2bc68b03c89c8eff8)

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

The 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
Copy the code

Analytical data

data_list = html_data['data']['list'] for i in data_list: Dit = {} dit [' stock code] = [' symbol '] I dit [' stock name '] = [' name '] I dit [' current price] = [' current '] I dit [' rise and fall of '] = [' CHG] I dit [' price / %] = [' percent] I dit [' year-to-date / %] = [' current_year_percent] I dit [' volume '] = [' volume '] I dit [' turnover '] = [' amount '] I dit [' turnover rate / %] = [' turnover_rate] I dit [' p/e TTM] = [' pe_ttm] I dit [' dividend yield / %] = [' dividend_yield] I dit [' market value '] = [' market_capital] I print(dit) 12345678910111213141516Copy the code
! [](https://p1-tt-ipv6.byteimg.com/large/pgc-image/b50d3e4e192e48b69696d9235039c8bd)

Save the data

Import CSV f = open(' stock data.csv ', mode='a', encoding=' UTF-8-sig ', newline='') csv_writer = csv.dictwriter (f, encoding=' utF-8-sig ', newline='') Fieldnames = [' stock code ', 'stock name', 'current price', 'or the forehead', 'price / %', 'year-to-date / %', 'volume', 'turnover', 'turnover rate / %', 'p/e TTM', 'dividend yield / %' ]) csv_writer.writeHeader () csv_writerow (dit) f.close() 123456Copy the code
! [](https://p6-tt-ipv6.byteimg.com/large/pgc-image/db37a62942c245c5ac26b5024c532705)