1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 猫眼电影票房爬取到MySQL中_爬虫之爬取猫眼电影专业版实时数据排行榜

猫眼电影票房爬取到MySQL中_爬虫之爬取猫眼电影专业版实时数据排行榜

时间:2019-11-02 19:23:40

相关推荐

猫眼电影票房爬取到MySQL中_爬虫之爬取猫眼电影专业版实时数据排行榜

猫眼电影专业版实时数据排行榜是个持续更新的用ajax写成的网站,一般而言,爬取它比较麻烦,需要使用ajax/js进行爬取,python中的requests库可以获取网站的ajax,再通过json库解析,就可以完成爬取。

#猫眼电影实时爬取

#/8/1

import os

import requests

import json

import time

import csv

#链接url

def get_to_link():

try:

r = requests.get("/promovie/api/box/second.json")

r.raise_for_status()

r.encoding = r.apparent_encoding

return r.text

except:

print("链接错误!!!")

return ''

#json化字符串

def json_text(text):

jd = json.loads(text)

return jd

#返回实时日期

def date_time(jd):

ja = jd['data']

date = ja['queryDate']#返回日期

alltime = ja['updateInfo'].split()[1]#返回时间

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]#上映时间

except:

days = '点映'

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

#创建文件夹

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 + '猫眼电影专业版实时数据.csv','a') as f:

writer = csv.writer(f)

writer.writerow(['日期',date,'','时间',alltime,'','总票房',moeny])

writer.writerow(['排名','影片名','上映时间(/天)','影片总票房','综合票房(/万)','票房占比(%)','排片场次','排片占比(%)','场均人次','上座率(%)'])

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:/数据/猫眼电影专业版数据/'

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,'时间',alltime,'总票房',moeny))

print('---'*46)

print('{:^6s}{:^20s}{:^10s}{:^12s}{:^12s}{:^10s}{:^10s}{:^6s}{:^6s}{:^6s}'.format('排名','影片名','上映时间(/天)','影片总票房(/亿)','综合票房(/万)','票房占比(%)','排片场次','排片占比(%)','场均人次','上座率(%)'))

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

© GitHub, Inc. Terms Privacy Security Statu

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。