python爬取貓眼電影top100榜單

項(xiàng)目目標(biāo):使用 requests 庫和正則表達(dá)式爬取貓眼電影 Top100 榜單,并保存為文件

目標(biāo)站點(diǎn)分析

  1. 電影分別在10個(gè)頁面中呈現(xiàn),第一個(gè)頁面 url 為 https://www.maoyan.com ,第二個(gè)頁面為 ,第三個(gè)為 ,其余頁面 url 以此類推。
  2. 每個(gè)電影的信息都在 <dd> 標(biāo)簽中,包括電影名稱、圖片地址、主演、上映時(shí)間以及評分
電影《霸王別姬》詳細(xì)代碼

編寫正則獲取信息

該頁面代碼比較簡單,代碼如下:

def parse_one_page(html):
'''正則解析網(wǎng)頁,獲取數(shù)據(jù)'''
    pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name"><a.*?>(.*?)</a>.*?star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)
    items = re.findall(pattern, html)
    # 整理輸出結(jié)果,按字典形式輸出
    for item in items:
        yield {
            'index': item[0],
            'image': item[1],
            'title': item[2],
            'actor': item[3].strip()[3:],
            'time': item[4][4:],
            'score': item[5] + item[6]
        }

保存爬取信息

以 json 格式保存爬取信息,代碼如下:

def write_to_file(content):
'''將結(jié)果保存到文件中, 并解決中文編碼問題'''

    with open('result.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')

輸出結(jié)果

部分輸出結(jié)果,index 為排名,actor 為主演, score 為評分,title 為電影名稱,time 為上映時(shí)間,image 為圖片url地址

{"actor": "張國榮,張豐毅,鞏俐", "score": "9.6", "index": "1", "title": "霸王別姬", "image": "http://p1.meituan.net/movie/20803f59291c47e1e116c11963ce019e68711.jpg@160w_220h_1e_1c", "time": ":1993-01-01(中國香港)"}
{"actor": "蒂姆·羅賓斯,摩根·弗里曼,鮑勃·岡頓", "score": "9.5", "index": "2", "title": "肖申克的救贖", "image": "http://p0.meituan.net/movie/__40191813__4767047.jpg@160w_220h_1e_1c", "time": ":1994-10-14(美國)"}
{"actor": "格利高利·派克,奧黛麗·赫本,埃迪·艾伯特", "score": "9.1", "index": "3", "title": "羅馬假日", "image": "http://p0.meituan.net/movie/23/6009725.jpg@160w_220h_1e_1c", "time": ":1953-09-02(美國)"}
{"actor": "讓·雷諾,加里·奧德曼,娜塔莉·波特曼", "score": "9.5", "index": "4", "title": "這個(gè)殺手不太冷", "image": "http://p0.meituan.net/movie/fc9d78dd2ce84d20e53b6d1ae2eea4fb1515304.jpg@160w_220h_1e_1c", "time": ":1994-09-14(法國)"}
{"actor": "萊昂納多·迪卡普里奧,凱特·溫絲萊特,比利·贊恩", "score": "9.5", "index": "5", "title": "泰坦尼克號", "image": "http://p0.meituan.net/movie/11/324629.jpg@160w_220h_1e_1c", "time": ":1998-04-03"}
{"actor": "馬龍·白蘭度,阿爾·帕西諾,詹姆斯·凱恩", "score": "9.3", "index": "6", "title": "教父", "image": "http://p0.meituan.net/movie/92/8212889.jpg@160w_220h_1e_1c", "time": ":1972-03-24(美國)"}
{"actor": "日高法子,坂本千夏,糸井重里", "score": "9.2", "index": "7", "title": "龍貓", "image": "http://p0.meituan.net/movie/99/678407.jpg@160w_220h_1e_1c", "time": ":1988-04-16(日本)"}
{"actor": "周星馳,鞏俐,鄭佩佩", "score": "9.2", "index": "8", "title": "唐伯虎點(diǎn)秋香", "image": "http://p0.meituan.net/movie/62/109878.jpg@160w_220h_1e_1c", "time": ":1993-07-01(中國香港)"}
{"actor": "柊瑠美,入野自由,夏木真理", "score": "9.3", "index": "9", "title": "千與千尋", "image": "http://p0.meituan.net/movie/9bf7d7b81001a9cf8adbac5a7cf7d766132425.jpg@160w_220h_1e_1c", "time": ":2001-07-20(日本)"}
{"actor": "費(fèi)雯·麗,羅伯特·泰勒,露塞爾·沃特森", "score": "9.2", "index": "10", "title": "魂斷藍(lán)橋", "image": "http://p0.meituan.net/movie/12/8506449.jpg@160w_220h_1e_1c", "time": ":1940-05-17(美國)"}

完整代碼和輸出文件請?jiān)L問:https://github.com/xieys 歡迎Follow和star

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容