Python3爬虫项目集:豆瓣电影排行榜top250
生活随笔
收集整理的這篇文章主要介紹了
Python3爬虫项目集:豆瓣电影排行榜top250
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 前言
- 爬蟲(chóng)概要
- 解析
- 代碼示例
- 數(shù)據(jù)存儲(chǔ)
Github地址:https://github.com/pasca520/Python3SpiderSet
前言
關(guān)于整理日常練習(xí)的一些爬蟲(chóng)小練習(xí),可用作學(xué)習(xí)使用。
爬取項(xiàng)目以學(xué)習(xí)為主,盡可能使用更多的模塊進(jìn)行練習(xí),而不是最優(yōu)解。
爬蟲(chóng)概要
| 爬取模塊 | request |
| 解析模塊 | BeautifulSoup |
| 存儲(chǔ)類(lèi)型 | list(方便存入數(shù)據(jù)庫(kù)) |
解析
BeautifulSoup參數(shù)我整理的一篇文章:https://blog.csdn.net/qinglianchen0851/article/details/102860741
代碼示例
# -*- coding: utf-8 -*-import requests from requests.exceptions import ReadTimeout, ConnectionError, RequestException from bs4 import BeautifulSoup# 爬蟲(chóng)主體 def get_page(url):headers = {'Connection': 'keep-alive','Cache-Control': 'max-age=0','User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3','Referer': 'https://maoyan.com/board',}try:response = requests.get(url=url, headers=headers).textreturn responseexcept ReadTimeout: # 訪問(wèn)超時(shí)的錯(cuò)誤print('Timeout')except ConnectionError: # 網(wǎng)絡(luò)中斷連接錯(cuò)誤print('Connect error')except RequestException: # 父類(lèi)錯(cuò)誤print('Error')# 解析網(wǎng)頁(yè) def parse_page(html):soup = BeautifulSoup(html, 'lxml')grid = soup.find(name="ol", attrs={"class": "grid_view"})movie_list = grid.find_all("li")for movie in movie_list:rank = movie.find(name="em").getText()name = movie.find(name="span", attrs={"class": "title"}).getText()rating_num = movie.find(name="span", attrs={"class": "rating_num"}).getText()# bd = movie.find(name="p").getText().strip().replace(' ', '\n').replace('...\n ', '...\n').replace(' / ', '\n').split('\n') # 頭皮發(fā)麻字符串分解系列,因?yàn)榫毩?xí)沒(méi)用 re,果然原生字符串處理麻煩的一匹,strip去除空格,replace替換,旨在將不同信息分類(lèi)存儲(chǔ)到不同的參數(shù),如導(dǎo)演、主演、上映時(shí)間、上映時(shí)間和電影類(lèi)型bd = movie.find(name="p").getText().strip().replace(' ', '\n').replace('...\n ', '...\n').replace(' / ', '\n').split('\n') # 頭皮發(fā)麻字符串分解系列,因?yàn)榫毩?xí)沒(méi)用 re,果然原生字符串處理麻煩的一匹,strip去除空格,replace替換,旨在將不同信息分類(lèi)存儲(chǔ)到不同的參數(shù),如導(dǎo)演、主演、上映時(shí)間、上映時(shí)間和電影類(lèi)型# 豆瓣有些主演沒(méi)有。。。賊蛋疼,為了簡(jiǎn)便只能寫(xiě)個(gè)爛代碼再增加一次了if len(bd) == 4:bd.insert(1, '沒(méi)爬到')inq = movie.find(name="span", attrs={"class": "inq"})# 處理 inq 為空的情況if not inq:inq = "暫無(wú)"else:inq = inq.getText()# 這里直接存儲(chǔ)到字典,方便存到數(shù)據(jù)庫(kù)douBanDict['rank'] = rankdouBanDict['name'] = namedouBanDict['director'] = bd[0]douBanDict['actor'] = bd[1]douBanDict['release_time'] = bd[2].strip() # 某些列表有空格,直接strip()去除空格douBanDict['country'] = bd[3]douBanDict['movie_types'] = bd[4]douBanDict['rating_num'] = rating_numdouBanDict['inq'] = inqdouBanList.append(str(douBanDict)) # 字典先轉(zhuǎn)為字符串再累加到列表中,否則無(wú)法字典值會(huì)一直變return douBanListif __name__ == '__main__':douBanList = []douBanDict = {}for start in range(0, 250, 25):url = 'https://movie.douban.com/top250?start={}&filter='.format(start)html = get_page(url)douBanList = parse_page(html)print(douBanList)數(shù)據(jù)存儲(chǔ)
直接是列表格式,同時(shí)包含各個(gè)電影信息的字典。
done!
總結(jié)
以上是生活随笔為你收集整理的Python3爬虫项目集:豆瓣电影排行榜top250的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 蓝桥杯2015年第六届C/C++省赛A组
- 下一篇: python中if brthon环境安装