很久沒有看電影了,近日國產(chǎn)科幻大片《流浪地球》正在火速登上榮耀榜,而在不久前拿下奧斯卡最佳影片獎的《綠皮書》在國內(nèi)上映后迅速贏得口碑票房雙豐收。
不如看看最近正在熱映的電影有哪些吧,順便看看評分和電影內(nèi)容,有時間的話就去電影院欣賞一下。
開發(fā)環(huán)境
- python3.7
- requests模塊
- re模塊
- pymongo
獲取分析
通過查看源代碼發(fā)現(xiàn)貓眼電影近日正在熱映的電影能直接請求源代碼來解析,這減少了很大的難度。
這里用正則表達(dá)式來解析近日正在熱映電影頁面。
#獲取貓眼電影正在熱映頁面信息
response = requests.get(url,headers = headers).text
category = re.findall(r'<li >.*?<a .*?href="\?catId.*?>(.*?)</a>.*?</li>',response,re.DOTALL) #匹配href為catid開頭的
titlename = re.findall(r'<div class="channel-detail movie-item-title".*?"(.*?)">.*?<a.*?>',response,re.DOTALL)
grades = re.findall(r'<div class="channel-detail movie-item-title".*?>.*?<div class="channel-detail channel-detail-orange">(.*?)</div>',response,re.DOTALL)
最后存入MongoDB數(shù)據(jù)庫。
#入庫操作
client = pymongo.MongoClient('127.0.0.1',port=27017)
dbaa = client.maoyan0
coll = dbaa.movie
coll.insert({'name':movie,'score':score,'content0':content0})
結(jié)果展示:

部分電影數(shù)據(jù)