赞
踩
嗨喽~大家好呀,这里是魔王呐 ❤ ~!
动态数据抓包
requests发送请求
json数据解析
python 3.8 运行代码
pycharm 2021.2 辅助敲代码
requests pip install requests
第三方模块安装:
win + R 输入cmd 输入安装命令 pip install 模块名 (如果你觉得安装速度比较慢, 你可以切换国内镜像源)
python资料、源码、教程\福利皆: 点击此处跳转文末名片获取
发送请求
获取数据
解析数据
保存数据
导入模块
import requests # 发送网络请求的 导入别人写好的功能
import csv # 写入数据的 内置模块
伪装
headers = {
'Cookie': 'UOR=www.baidu.com,tousu.sina.com.cn,; SINAGLOBAL=175.0.61.77_1657884424.966721; U_TRS1=0000004e.e788d5bf.6321e608.840542e4; Apache=175.0.61.120_1669203295.444472; ULV=1669203299775:10:6:4:175.0.61.120_1669203295.444472:1669203296517',
'Host': 'api.sports.sina.com.cn',
'Referer': 'h****.cn/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
请求链接
url = 'http://****.cn/?p=sports&s=sport_client&a=index&_sport_t_=football&_sport_s_=opta&_sport_a_=teamStatics&show_players=1&type=108&season=2017&id=960&dpc=1'
1. 发送请求
response = requests.get(url=url, headers=headers)
2. 获取数据
json_data = response.json()
3. 解析数据 提取数据
什么是字典?
数据容器 用来存数据 通过:前面的拼音 取到:后面的值
{"A":"不","E":"额","C":"存","D":"的"}
[{}, {}, {}]: 列表 数据容器 存数据
players = json_data['result']['data']['players']
# 完整源码、解答、教程、安装包等资料加V:qian97378免费领
wb以二进制写入
f = open('日本球队.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['player_name_cn', 'count', 'mins_played', 'goal_assist', 'goals', 'total_pass', 'accurate_pass'])
for player in players:
player_name_cn = player['player_name_cn'] # 球员名称
完整源码、解答、教程、安装包等资料加V:qian97378免费领
count = player['count'] # 场次
mins_played = player['mins_played'] # 出场时间
goal_assist = player['goal_assist'] # 助攻
goals = player['goals'] # 进球
total_pass = player['total_pass'] # 传球
accurate_pass = player['accurate_pass'] # 传准
csv_writer.writerow([player_name_cn, count, mins_played, goal_assist, goals, total_pass, accurate_pass])
感谢你观看我的文章呐~本次航班到这里就结束啦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。