foreword
Today I plan to come to the whole stock market and simply collect some stock data
If you are interested in this, let's have a look.
Prepare
Development Environment & Third Party Modules
- Interpreter version: python 3.8
- Code editor: pycharm 2021.2
- requests: pip install requests crawler
- pyecharts: pip install pyecharts data analysis
- pandas: pip install pandas data analysis
The basic process of crawlers
1. Thinking analysis
What data to collect and how to collect
Find the data source: Find the location of the data from the network
https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page=1&size=30&order= desc&orderby=percent&order_by=percent&market=CN&type=sh_sz
2. Code implementation (normally there are several steps to implement a crawler code)
- send request
- retrieve data
- Analytical data
- save data
Implementation code
import requests # 第三方模块
import csv
1. Send request
response = requests.get(url=url, headers=headers)
2. Get data
json_data = response.json()
3. Parse the data
Extract data to get out the desired content
'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:770699889
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
data_list = json_data['data']['list']
# data_list[0]
# data_list[1]
for i in range(0, len(data_list)):
symbol = data_list[i]['symbol']
name = data_list[i]['name']
current = data_list[i]['current']
chg = data_list[i]['chg']
percent = data_list[i]['percent']
current_year_percent = data_list[i]['current_year_percent']
volume = data_list[i]['volume']
amount = data_list[i]['amount']
turnover_rate = data_list[i]['turnover_rate']
pe_ttm = data_list[i]['pe_ttm']
dividend_yield = data_list[i]['dividend_yield']
market_capital = data_list[i]['market_capital']
print(symbol, name, current, chg, percent, current_year_percent, volume, amount, turnover_rate, pe_ttm, dividend_yield, market_capital)
4. Save data
csv_writer.writerow([symbol, name, current, chg, percent, current_year_percent, volume, amount, turnover_rate, pe_ttm, dividend_yield, market_capital])
At last
My friends are learning python, and sometimes they don't know how to learn and where to start. After mastering some basic grammar or doing two cases, I don't know how to take the next step, and I don't know how to learn more advanced knowledge.
So for these friends, I have prepared a lot of free video tutorials, PDF e-books, and source codes! Just pick up the business card at the end of the article!
Today's sharing ends here
By the way, I would like to recommend some Python video tutorials for you, hoping to help you:
Python zero-based teaching collection
If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you
think the article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/