Python爬取猫眼电影数据并对其进行数据可视化
生活随笔
收集整理的這篇文章主要介紹了
Python爬取猫眼电影数据并对其进行数据可视化
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
如果大家經(jīng)常閱讀Python爬蟲(chóng)相關(guān)的公眾號(hào),都會(huì)是以爬蟲(chóng)+數(shù)據(jù)分析的形式展現(xiàn)的,這樣很有趣,圖表也很不錯(cuò),今天了,我就來(lái)分享上一次在培訓(xùn)中的一個(gè)作品:貓眼電影爬蟲(chóng)及分析。
爬蟲(chóng)分析
這里是獲取的是top100的電影數(shù)據(jù),進(jìn)行了跨頁(yè)爬蟲(chóng),獲取的字段:電影名,主演,上映時(shí)間,評(píng)分,電影類(lèi)型和時(shí)長(zhǎng)。最后保存在csv文件中。
爬蟲(chóng)代碼
import requests from lxml import etree import csv''' 遇到不懂的問(wèn)題?Python學(xué)習(xí)交流群:1136201545滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載! ''' headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' }def get_url(url):res = requests.get(url,headers=headers)html = etree.HTML(res.text)infos = html.xpath('//dl[@class="board-wrapper"]/dd')for info in infos:name = info.xpath('div/div/div[1]/p[1]/a/text()')[0]info_url = 'http://maoyan.com' + info.xpath('div/div/div[1]/p[1]/a/@href')[0]star = info.xpath('div/div/div[1]/p[2]/text()')[0].strip()release_time = info.xpath('div/div/div[1]/p[3]/text()')[0].strip()score_1 = info.xpath('div/div/div[2]/p/i[1]/text()')[0]score_2 = info.xpath('div/div/div[2]/p/i[2]/text()')[0]score = score_1 + score_2# print(name,star,release_time,score,info_url)get_info(info_url,name,star,release_time,score)def get_info(url,name,star,time,score):res = requests.get(url, headers=headers)html = etree.HTML(res.text)style = html.xpath('/html/body/div[3]/div/div[2]/div[1]/ul/li[1]/text()')[0]long_time = html.xpath('/html/body/div[3]/div/div[2]/div[1]/ul/li[2]/text()')[0].split('/')[1].strip()print(name,star,time,score,style,long_time)writer.writerow([name,star,time,score,style,long_time])if __name__ == '__main__':fp = open('maoyan_2.csv','w',encoding='utf-8',newline='')writer = csv.writer(fp)writer.writerow(['name','star','time','score','style','long_time'])urls = ['http://maoyan.com/board/4?offset={}'.format(str(i)) for i in range(0, 100, 10)]for url in urls:get_url(url)數(shù)據(jù)分析
總體情況
100部電影,平均得分9.0,平均電影時(shí)長(zhǎng)128.63。
電影年份趨勢(shì)
電影年份趨勢(shì)不大,規(guī)律不太明顯。
電影月份
大家看電影都知道,電影基本在假期上映更有熱度,這里統(tǒng)計(jì)出來(lái),發(fā)現(xiàn)下半年的電影比上半年電影好很多~
地區(qū)
中國(guó)和美國(guó)還是占了很多的,韓國(guó)和日本電影也很不錯(cuò)~
電影類(lèi)型
電影大部分都是劇情的,愛(ài)情才是真諦啊。
總結(jié)
以上是生活随笔為你收集整理的Python爬取猫眼电影数据并对其进行数据可视化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 权限汇总大全
- 下一篇: Lifecycle+Retrofit+R