foreword
Hello! Hello everyone, this is the Demon King~
Knowledge points:
- requests send request
- Use of developer tools
- json type data parsing
- Use of regular expressions
Module installation:
Press and hold the keyboard win + r, enter cmd and press Enter to open the command line window, enter the pip install module name in it
Development environment:
- Version: python 3.8
- Editor: pycharm 2021.2
Code
- send request
- retrieve data
- Analytical data
- save data
code
It is better for me to delete the URL in the code than to review it. If you want a friend, you can read the comments or privately chat with me to get it~
import requests
import re
# 伪装
headers = {
'Cookie': 'qgqp_b_id=7b7cfe791fce1724e930884be192c85e; HAList=a-sz-300059-%u4E1C%u65B9%u8D22%u5BCC%2Cty-100-HSI-%u6052%u751F%u6307%u6570%2Cty-1-000001-%u4E0A%u8BC1%u6307%u6570; em_hq_fls=js; intellpositionL=1522.39px; intellpositionT=3367px; ASP.NET_SessionId=qevhbz22qxoqxpbwclsxzctm; _adsame_fullscreen_16928=1; st_si=42277447983105; st_asi=delete; st_pvi=25714906615245; st_sp=2022-02-08%2022%3A12%3A10; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=2; st_psi=20220407142006395-112200312936-7700776192',
'Host': 'fund.eastmoney.com',
'Referer': '',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
}
with open('股票.csv', mode='a', encoding='utf-8') as f:
f.write('基金代码,基金简称,基金英文,日期,单位净值,累计净值,日增长率,近一周,近一个月,近三个月,近六个月,近一年,近两年,近三年,近年来,成立来,上市日期,,,,手续费,,')
f.write('\n')
# 1. 发送请求
for page in range(1, 214):
url = f''
response = requests.get(url=url, headers=headers)
# <Response [200]>: 请求成功
# 2. 获取数据
data = response.text
# 3. 解析数据
datas = re.findall('datas:\[(.*)\],', data)[0]
# eval(): 字符串去除引号 '{}','[]', '字符串' 元组
data_tuple = eval(datas)
for tuple_ in data_tuple:
print(tuple_)
# 4. 保存数据
with open('股票.csv', mode='a', encoding='utf-8') as f:
f.write(tuple_)
f.write('\n')
video tutorial
Python case combat: collecting network stock data~
epilogue
Well, this article of mine ends here!
If you have more suggestions or questions, feel free to comment or private message me! Let's work hard together (ง •_•)ง
Follow the blogger if you like it, or like and comment on my article! ! !