貓眼電影專業(yè)版實(shí)時(shí)數(shù)據(jù)排行榜是個(gè)持續(xù)更新的用ajax寫成的網(wǎng)站,一般而言具伍,爬取它比較麻煩髓绽,需要使用ajax/js進(jìn)行爬取,python中的requests庫可以獲取網(wǎng)站的ajax竭鞍,再通過json庫解析,就可以完成爬取橄镜。
#貓眼電影實(shí)時(shí)爬取
#2017/8/1
import os
import requests
import json
import time
import csv
#鏈接url
def get_to_link():
try:
r = requests.get("https://box.maoyan.com/promovie/api/box/second.json")
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
print("鏈接錯(cuò)誤Y丝臁!洽胶!")
return ''
#json化字符串
def json_text(text):
jd = json.loads(text)
return jd
#返回實(shí)時(shí)日期
def date_time(jd):
ja = jd['data']
date = ja['queryDate']#返回日期
alltime = ja['updateInfo'].split()[1]#返回時(shí)間
money = ja['totalBox'] + ja['totalBoxUnit']#返回總票房
return date,alltime,money
#返回影片票房
def movie_price(jd):
jl = jd['data']['list']
for i,jls in enumerate(jl,1):
name = jls['movieName']#影片名
try:
days = jls['releaseInfo'][2]#上映時(shí)間
except:
days = '點(diǎn)映'
totalmoney = jls['sumBoxInfo']#影片總票房
mainmoney = jls['boxInfo']#綜合票房
moneyrate = jls['boxRate']#票房占比
shownumber = jls['showInfo']#排片場次
showrate = jls['showRate']#排片占比
people = jls['avgShowView']#場均人次
showpeople = jls['avgSeatView']#上座率
yield i,name,days,totalmoney,mainmoney,moneyrate,shownumber,showrate,people,showpeople
#創(chuàng)建文件夾
def makeasocket(path):
if not os.path.exists(path):
os.makedirs(path)
#保存到csv中
def save_to_csv(path,date,alltime,moeny,movie_price):
with open(path + '貓眼電影專業(yè)版實(shí)時(shí)數(shù)據(jù).csv','a') as f:
writer = csv.writer(f)
writer.writerow(['日期',date,'','時(shí)間',alltime,'','總票房',moeny])
writer.writerow(['排名','影片名','上映時(shí)間(/天)','影片總票房','綜合票房(/萬)','票房占比(%)','排片場次','排片占比(%)','場均人次','上座率(%)'])
for movie in movie_price:
writer.writerow([movie[0],movie[1],movie[2],movie[3],movie[4],movie[5],movie[6],movie[7],movie[8],movie[9]])
def main():
path = 'D:/數(shù)據(jù)/貓眼電影專業(yè)版數(shù)據(jù)/'
makeasocket(path)
while True:
text = get_to_link()
jd = json_text(text)
date,alltime,moeny = date_time(jd)
print('***'*46)
print('{:>10s}:{}{:>10s}:{}{:>10s}:{}'.format('日期',date,'時(shí)間',alltime,'總票房',moeny))
print('---'*46)
print('{:^6s}{:^20s}{:^10s}{:^12s}{:^12s}{:^10s}{:^10s}{:^6s}{:^6s}{:^6s}'.format('排名','影片名','上映時(shí)間(/天)','影片總票房(/億)','綜合票房(/萬)','票房占比(%)','排片場次','排片占比(%)','場均人次','上座率(%)'))
print('---'*46)
for movie in movie_price(jd):
print('{:^6d}{:^20s}{:^20s}{:^20s}{:^12s}{:^11s}{:^13s}{:^10s}{:^10s}{:^10s}'.format(movie[0],movie[1],movie[2],movie[3],movie[4],movie[5],movie[6],movie[7],movie[8],movie[9]))
print('---'*46)
save_to_csv(path,date,alltime,moeny,movie_price(jd))
time.sleep(3)
if __name__ == "__main__":
main()
Contact GitHub API Training Shop Blog About
? 2017 GitHub, Inc. Terms Privacy Security Statu