前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取
基本环境配置
- python 3.6
- pycharm
- requests
- csv
相关模块pip安装即可
目标网站
数据接口
请求网页获取数据
import requests
url = 'http://piaofang.maoyan.com/dashboard-ajax/movie'
params = {
}
cookies = {
}
headers = {
}
response = requests.get(url=url, params=params, headers=headers, cookies=cookies)
html_data = response.json()
pprint.pprint(html_data)
解析数据
movieList = html_data['movieList']['list']
dit = {}
for i in movieList:
dit['电影名'] = i['movieInfo']['movieName']
dit['票房'] = i['sumBoxDesc']
dit['票房占比'] = i['boxRate']
dit['排片占比'] = i['showCountRate']
dit['上映周期'] = i['movieInfo']['releaseInfo']
pprint.pprint(dit)
保存数据
import csv
f = open('data.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(f, fieldnames=['电影名', '票房', '票房占比', '排片占比', '上映周期'])
csv_write.writeheader()
f.close()