日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2023/12/14 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 猫眼电影票房爬取到MySQL中_爬虫之爬取猫眼电影专业版实时数据排行榜 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

貓眼電影專(zhuān)業(yè)版實(shí)時(shí)數(shù)據(jù)排行榜是個(gè)持續(xù)更新的用ajax寫(xiě)成的網(wǎng)站,一般而言,爬取它比較麻煩,需要使用ajax/js進(jìn)行爬取,python中的requests庫(kù)可以獲取網(wǎng)站的ajax,再通過(guò)json庫(kù)解析,就可以完成爬取。

#貓眼電影實(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ò)誤!!!")

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']#排片場(chǎng)次

showrate = jls['showRate']#排片占比

people = jls['avgShowView']#場(chǎng)均人次

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 + '貓眼電影專(zhuān)業(yè)版實(shí)時(shí)數(shù)據(jù).csv','a') as f:

writer = csv.writer(f)

writer.writerow(['日期',date,'','時(shí)間',alltime,'','總票房',moeny])

writer.writerow(['排名','影片名','上映時(shí)間(/天)','影片總票房','綜合票房(/萬(wàn))','票房占比(%)','排片場(chǎng)次','排片占比(%)','場(chǎng)均人次','上座率(%)'])

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ù)/貓眼電影專(zhuān)業(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í)間(/天)','影片總票房(/億)','綜合票房(/萬(wàn))','票房占比(%)','排片場(chǎng)次','排片占比(%)','場(chǎng)均人次','上座率(%)'))

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

總結(jié)

以上是生活随笔為你收集整理的猫眼电影票房爬取到MySQL中_爬虫之爬取猫眼电影专业版实时数据排行榜的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。