前言
大家早好、午好、晚好吖~
开发环境 & 第三方模块:
-
解释器版本: python 3.8
-
代码编辑器: pycharm 2021.2
-
requests: pip install requests 爬虫
-
pyecharts: pip install pyecharts 数据分析
-
pandas: pip install pandas 数据分析
如果安装python第三方模块:
-
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
-
在pycharm中点击Terminal(终端) 输入安装命令
如不会添加可以添加Q裙;832157862找管理员小姐姐领取哦~(源码、解答也可找哦)
代码实现:
-
发送请求
-
获取数据
-
解析数据
-
保存数据
代码
因审核机制原因,我把网址里的一些东西删掉了,小可耐们可以自己添加一下哈,很容易的
具体爬取的网站我会在评论区打出,你们注意查看哦~
如果有不太会改或者有点点小懒惰的小可耐也可以私信我,我发你呐~ ?
也可直接添加Q裙;832157862找管理员小姐姐领取哦~(教程、解答也可找哦)
导入模块
import requests # 发送请求 第三方模块
import csv # 内置模块 不需要额外安装
加伪装
# 伪装
headers = {
# 用户基本信息
'Cookie': 's=bq119wflib; xq_a_token=fb81acf5170aad073886d6f4710230100565c245; xq_r_token=7b20eba2d7f8fb7ff92d34226863aebb43cc1bfc; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY2MDUyMDE1MywiY3RtIjoxNjU4NDg4MDQ2MDE0LCJjaWQiOiJkOWQwbjRBWnVwIn0.Oy2cyGJLCPutPUQW07bInBWrDMu3nSMBLiDzpZeLJP51GGa7jcLA2Cbv9AGmK6dDQxrHXWVdeGSRU207sBCQ8LyBHzh86zX2jOM9O8a6vmiWWWE1vVrika0-IPC061IqnDWoNX6UYglkOTsROyMBl1ToFMSwnbLtSYjFHDlCrS4LaWccbUoTN1WI8m0gQMJ7uQRuzcrwT3Qz9OomgP2WuEstcW4cWlkUBQy7015lGkUCdAfHSpKw2iKpOIBbgyilylPjsFavfhdhlzWaMaL5v8RVo1pgk-EJf_l3dH8lD0aQSL2ivrJ7KI63LTQLgccVXs449M9wPc_PRGLlwQm94Q; u=701658488083353; cookiesu=701658488083353; device_id=90ec0683f24e4d1dd28a383d87fa03c5; Hm_lvt_1db88642e346389874251b5a1eded6e3=1658488084; __utma=1.1460934076.1658488091.1658488091.1658488091.1; __utmc=1; __utmz=1.1658488091.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lpvt_1db88642e346389874251b5a1eded6e3=1658488099; acw_tc=2760779916584917111362559e5892bf3e77c79f159bb96b31b388f4669de3',
# 域名
'Host': '.com',
# 防盗链
'Referer': 'https://.com/hq',
# 浏览器基本信息
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
f = open('股票.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['symbol', 'name', 'current', 'chg', 'percent', 'current_year_percent', 'volume', 'amount', 'turnover_rate', 'pe_ttm', 'dividend_yield', 'market_capital'])
for page in range(1, 162):
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
发送请求
# 变量 专门用来接收 网页链接
url = f'https://.com/service/v5/stock/screener/quote/list?page={page}&size=30&order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz&_=1658491936321'
response = requests.get(url=url, headers=headers)
# <Response [403]>: 无法定位到资源 没有权限
# <Response [200]>: 请求成功
- 3
- 4
- 5
获取数据
json_data = response.json()
- 1
解析数据
data_list = json_data['data']['list']
# 循环 节省代码量的
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)
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
保存数据
# mode='a': 追加写入
# encoding='utf-8': 编码方式
# newline='': 数据空行
f = open('雪球股票.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow([symbol, name, current, chg, percent, current_year_percent, volume, amount, turnover_rate, pe_ttm, dividend_yield, market_capital])
- 3
- 4
- 5
- 6
尾语
好了,我的这篇文章写到这里就结束啦!
有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!