python正則匹配抓取豆瓣電影鏈接和評論代碼分享
import urllib.request
import re
import time
def movie(movieTag):
tagUrl=urllib.request.urlopen(url)
tagUrl_read = tagUrl.read().decode('utf-8')
return tagUrl_read
def subject(tagUrl_read):
'''
這里還存在問題:
①這只針對單獨(dú)的一頁進(jìn)行排序,而沒有對全部頁面的電影進(jìn)行排序
②下次更新添加電影鏈接,考慮添加電影海報(bào)
③需要追加列表
④導(dǎo)入到本地txt或excel中
⑤在匹配電影名字時(shí)是否可以同時(shí)匹配鏈接與名字、評分、評論組成數(shù)組
⑥
'''
#正則表達(dá)式匹配電影的名字(鏈接)、評分與評論
nameURL = re.findall(r'(http://movie.douban.com/subject/[0-9.]+)\/"\s+title="(.+)"',tagUrl_read)
scoreURL = re.findall(r'<span\s+class="rating_nums">([0-9.]+)<\/span>',tagUrl_read)
evaluateURL = re.findall(r'<span\s+class="pl">\((\w+)人評價(jià)\)<\/span>',tagUrl_read)
movieLists = list(zip(nameURL,scoreURL,evaluateURL))
newlist.extend(movieLists)
return newlist
#用quote處理特殊(中文)字符
movie_type = urllib.request.quote(input('請輸入電影類型(如劇情、喜劇、懸疑):'))
page_end=int(input('請輸入搜索結(jié)束時(shí)的頁碼:'))
num_end=page_end*20
num=0
page_num=1
newlist=[]
while num<num_end:
url=r'http://movie.douban.com/tag/%s?start=%d'%(movie_type,num)
movie_url = movie(url)
subject_url=subject(movie_url)
num=page_num*20
page_num+=1
else:
#使用sorted函數(shù)對列表進(jìn)行排列,reverse參數(shù)為True時(shí)升序,默認(rèn)或False時(shí)為降序, key=lambda還不是很明白這里的原理
movieLIST = sorted(newlist, key=lambda movieList : movieList[1],reverse = True)
for movie in movieLIST:
print(movie)
time.sleep(3)
print('結(jié)束')
相關(guān)文章
Django 實(shí)現(xiàn)將圖片轉(zhuǎn)為Base64,然后使用json傳輸
這篇文章主要介紹了Django 實(shí)現(xiàn)將圖片轉(zhuǎn)為Base64,然后使用json傳輸,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03解決Python串口接收無標(biāo)識不定長數(shù)據(jù)
這篇文章主要介紹了解決Python串口接收無標(biāo)識不定長數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python如何使用神經(jīng)網(wǎng)絡(luò)進(jìn)行簡單文本分類
這篇文章主要介紹了Python如何使用神經(jīng)網(wǎng)絡(luò)進(jìn)行簡單文本分類,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02Python實(shí)現(xiàn)telnet服務(wù)器的方法
這篇文章主要介紹了Python實(shí)現(xiàn)telnet服務(wù)器的方法,涉及Python通過Telnet連接服務(wù)器的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07