目標網站:
https://piaofang.maoyan.com/dashboard/movie

資料介面:
F12大法開啟:
觀察唄,哪個像就點進去preview一下:

有眼感了一下子就抓到了:

點進response,右鍵open一下:
需要的資訊都在了,接下來手撕代碼:

完整代碼:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# 貓眼票房:https://piaofang.maoyan.com/dashboard
import datetime
import os
import time
import requests
class PF(object):
def __init__(self):
self.url = 'https://piaofang.maoyan.com/dashboard-ajax?orderType=0&uuid=173d6dd20a2c8-0559692f1032d2-393e5b09-1fa400-173d6dd20a2c8&riskLevel=71&optimusCode=10'
self.headers = {
"Referer": "https://piaofang.maoyan.com/dashboard",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
}
def main(self):
'''
主程式,列印最終結果
:return:
'''
while True:
# 需在dos命令下運行此檔案,才能清屏
os.system('cls')
result_json = self.get_parse()
if not result_json:
break
results = self.parse(result_json)
# 獲取時間
calendar = result_json['calendar']['serverTimestamp']
t = calendar.split('.')[0].split('T')
t = t[0] + " " + (datetime.datetime.strptime(t[1], "%H:%M:%S") + datetime.timedelta(hours=8)).strftime(
"%H:%M:%S")
print("北京時間:", t)
x_line = '-' * 155
# 總票房
total_box = result_json['movieList']['data']['nationBoxInfo']['nationBoxSplitUnit']['num']
# 總票房單位
total_box_unit = result_json['movieList']['data']['nationBoxInfo']['nationBoxSplitUnit']['unit']
print(f"今日總票房: {total_box} {total_box_unit}", end=f'\n{x_line}\n')
# print("{:^10}\t{:^23}".format("企業ID", "企業名稱"))
print('電影名稱'.ljust(14), '綜合票房'.ljust(11), '票房占比'.ljust(13), '場均上座率'.ljust(11), '場均人次'.ljust(11),
'排片場次'.ljust(12),
'排片占比'.ljust(12), '累積總票房'.ljust(11), '上映天數', sep='\t', end=f'\n{x_line}\n')
for result in results:
print(
result['movieName'][:10].ljust(9), # 電影名稱
result['boxSplitUnit'][:8].rjust(10), # 綜合票房
result['boxRate'][:8].rjust(13), # 票房占比
result['avgSeatView'][:8].rjust(13), # 場均上座率
result['avgShowView'][:8].rjust(13), # 場均人次
result['showCount'][:8].rjust(13), # '排片場次'
result['showCountRate'][:8].rjust(13), # 排片占比
result['sumBoxDesc'][:8].rjust(13), # 累積總票房
result['releaseInfo'][:8].rjust(13), # 上映資訊
sep='\t', end='\n\n'
)
break # 把break注釋掉,列印的是所有電影實時票房,否則只列印榜首
time.sleep(4)
def get_parse(self):
'''
網頁是否成功獲取,頻繁操作會有驗證
:return:
'''
try:
response = requests.get(self.url, headers=self.headers)
if response.status_code == 200:
# print("success!")
return response.json()
except requests.ConnectionError as e:
print("ERROR:", e)
return None
def parse(self, result_json):
'''
獲取資料
:return:
'''
if result_json:
movies = result_json['movieList']['data']['list']
# movies = [{},{},{}]
# 場均上座率, 場均人次, 票房占比, 電影名稱,
# 上映資訊(上映天數), 排片場次, 排片占比, 綜合票房,累積總票房
ticks = ['avgSeatView', 'avgShowView', 'boxRate', 'movieName',
'releaseInfo', 'showCount', 'showCountRate', 'boxSplitUnit', 'sumBoxDesc']
for movie in movies:
self.piaofang = {}
for tick in ticks:
# 數字和單位分開需要join
if tick == 'boxSplitUnit':
movie[tick] = ''.join([str(i) for i in movie[tick].values()])
# 多層字典嵌套
if tick == 'movieName' or tick == 'releaseInfo':
movie[tick] = movie['movieInfo'][tick]
if movie[tick] == '':
movie[tick] = '此項資料為空'
self.piaofang[tick] = str(movie[tick])
yield self.piaofang
if __name__ == '__main__':
pf = PF()
pf.main()
抓取結果:


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264841.html
標籤:python
