前言
嗨嗨,最近看球赛的朋友多吗
emm怎么说,我对这个虽然兴趣不是很大
但是还是想跟朋友赌赌,自己对这些球员也不是很熟悉,索性叫我的好同事帮我用Python采集了各国球员的一些信息,没料到竟预测成功了!
这次日本队还是有点东西的
知识点
- 动态数据抓包
- requests发送请求
- json数据解析
开发环境
- python 3.8 运行代码
- pycharm 2021.2 辅助敲代码
- requests pip install requests
实现代码:
- 发送请求
- 获取数据
- 解析数据
- 保存数据
import requests # 发送网络请求的 导入别人写好的功能
import csv # 写入数据的 内置模块python学习交流Q群:770699889 ### 源码领取
# 伪装
headers = {'Cookie': '*****; ULV=1669203299775:10:6:4:175.0.61.120_1669203295.444472:1669203296517','Host': 'api.sports.sina.com.cn','Referer': 'http://****.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'response = requests.get(url=url, headers=headers)json_data = response.json()players = json_data['result']['data']['players']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'] # 球员名称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])
最后
今天的分享到这里就结束了 ,感兴趣的朋友也可以去试试哈
顺便给大家推荐一些Python视频教程,希望对大家有所帮助:
Python零基础教学合集
对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/