大家好,我是python222小鋒老師。
近日鋒哥又卷了一波Python實戰(zhàn)課程-批量爬取豆瓣電影排行信息,主要是鞏固下Python爬蟲基礎(chǔ)
視頻版教程:
Python爬蟲實戰(zhàn)-批量爬取豆瓣電影排行信息 視頻教程_嗶哩嗶哩_bilibiliPython爬蟲實戰(zhàn)-批量爬取豆瓣電影排行信息 視頻教程作者:小鋒老師官網(wǎng):www.python222.com, 視頻播放量 344、彈幕量 0、點贊數(shù) 13、投硬幣枚數(shù) 7、收藏人數(shù) 18、轉(zhuǎn)發(fā)人數(shù) 0, 視頻作者 java1234官方, 作者簡介 公眾號:java1234 微信:java9266,相關(guān)視頻:Python爬蟲實戰(zhàn)-批量爬取下載網(wǎng)易云音樂,Python爬蟲實戰(zhàn)-批量爬取美女圖片網(wǎng)下載圖片 視頻教程,這個視頻可能會得罪很多人..終極爬蟲JS逆向教程!突破反爬蟲防御的終極指南,從入門到實戰(zhàn),就沒有爬不了的網(wǎng)站!,【Python爬蟲教程】你敢學(xué)我就敢發(fā)!300集從入門到入獄(完整版)膽小勿學(xué)!全程干貨無廢話,學(xué)完即可兼職接單!,2024 一天掌握python爬蟲【基礎(chǔ)篇】 涵蓋 requests、beautifulsoup、selenium 【無廢話版】,【Python爬蟲】三分鐘教你免費下載全網(wǎng)VIP音樂,音樂一鍵下載免費聽,告別付費時代,小白也能學(xué)得會,附源碼!,【Python爬蟲】手把手教你爬取百度文庫PPT文檔,破解百度文庫收費限制,下載PPT再也沒花過錢!,【B站第一】清華大佬196小時講完的Python入門學(xué)習(xí)教程!從小白到大神!整整300集,全干貨無廢話,學(xué)完即可就業(yè)!允許白嫖??!,【2023百度文庫VIP文檔PPT免費下載】Python白嫖百度文庫付費VIP文檔,破解百度文庫收費限制,零基礎(chǔ)白嫖教程?。。?,【Python教程】一分鐘輕松實現(xiàn)觀影自由,教你用Python免費看電影,代碼可分享 | Python爬蟲教程https://www.bilibili.com/video/BV1aN411u7o5/
爬蟲目標網(wǎng)站:
https://movie.douban.com/top250
經(jīng)過分析,一共10頁,第二頁,第二頁,...,第10頁的規(guī)律是:
分頁規(guī)律 第N頁
https://movie.douban.com/top250?start=25*(N-1)&filter=
爬取網(wǎng)頁,解析數(shù)據(jù),處理數(shù)據(jù),我們最終把數(shù)據(jù)存入Excel。
因為涉及到分頁,所以我們要對解析url,解析網(wǎng)頁,導(dǎo)出Excel代碼,進行封裝,方便復(fù)用。
解析請求,爬取網(wǎng)頁方法:
def crawl_html(url):
"""
解析請求,爬取網(wǎng)頁
:param url: 請求地址
:return: 網(wǎng)頁源碼
"""
response = requests.get(url=url, headers=headers)
return response.text
解析網(wǎng)頁源碼方法:
def parse_html(html):
"""
解析網(wǎng)頁源碼
:param html: 頁面源碼
:return: 頁面 電影對象信息列表 [ {'':''},{},{} ]
"""
# 實例化soup
soup = BeautifulSoup(html, "lxml")
# 獲取所有電影DOM
movie_list = soup.select("ol.grid_view li")
# print(movie_list)
# 電影數(shù)據(jù)對象列表
movie_data_list = []
for movie in movie_list:
try:
rank = movie.select_one("div.pic em").text # 獲取排名
title = movie.select_one("div.info span.title").text # 獲取電影名稱
info = movie.select_one("div.bd p").text.strip() # 獲取電影描述信息
rating_num = movie.select_one("div.star span.rating_num").text # 獲取評分
comment_count = movie.select("div.star span")[3].text.replace("人評價", "") # 獲取評論數(shù)
quete_dom = movie.select_one("p.quote span.inq")
quote = ""
if quete_dom:
quote = quete_dom.text
# quote = movie.select_one("p.quote span.inq").text # 獲取電影描述
movie_data_list.append({
"rank": rank,
"title": title,
"info": info,
"rating_num": rating_num,
"comment_count": comment_count,
"quote": quote
})
except:
print(movie.select_one("div.pic em").text, "異常", traceback.print_exc())
continue
return movie_data_list
導(dǎo)出Excel方法:借助pandas庫文章來源:http://www.zghlxwxcb.cn/news/detail-753967.html
def export_excel(datas):
"""
導(dǎo)出數(shù)據(jù)到Excel
:param datas: 數(shù)據(jù)
:return:
"""
df = pd.DataFrame(datas)
df.to_excel("豆瓣電影TOP250.xlsx")
完整源碼參考:具體代碼解釋,參考帖子頂部的視頻教程。文章來源地址http://www.zghlxwxcb.cn/news/detail-753967.html
"""
豆瓣電影 Top 250 抓取電影信息,存到excel里面
使用requests爬取網(wǎng)頁,使用bs4解析數(shù)據(jù),使用pandas將數(shù)據(jù)寫入Excel
目標網(wǎng)頁:https://movie.douban.com/top250
分頁規(guī)律 第N頁
https://movie.douban.com/top250?start=25*(N-1)&filter=
作者:小鋒老師
官網(wǎng):www.python222.com
"""
import traceback
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
}
url = "https://movie.douban.com/top250?start=0&filter="
def crawl_html(url):
"""
解析請求,爬取網(wǎng)頁
:param url: 請求地址
:return: 網(wǎng)頁源碼
"""
response = requests.get(url=url, headers=headers)
return response.text
def parse_html(html):
"""
解析網(wǎng)頁源碼
:param html: 頁面源碼
:return: 頁面 電影對象信息列表 [ {'':''},{},{} ]
"""
# 實例化soup
soup = BeautifulSoup(html, "lxml")
# 獲取所有電影DOM
movie_list = soup.select("ol.grid_view li")
# print(movie_list)
# 電影數(shù)據(jù)對象列表
movie_data_list = []
for movie in movie_list:
try:
rank = movie.select_one("div.pic em").text # 獲取排名
title = movie.select_one("div.info span.title").text # 獲取電影名稱
info = movie.select_one("div.bd p").text.strip() # 獲取電影描述信息
rating_num = movie.select_one("div.star span.rating_num").text # 獲取評分
comment_count = movie.select("div.star span")[3].text.replace("人評價", "") # 獲取評論數(shù)
quete_dom = movie.select_one("p.quote span.inq")
quote = ""
if quete_dom:
quote = quete_dom.text
# quote = movie.select_one("p.quote span.inq").text # 獲取電影描述
movie_data_list.append({
"rank": rank,
"title": title,
"info": info,
"rating_num": rating_num,
"comment_count": comment_count,
"quote": quote
})
except:
print(movie.select_one("div.pic em").text, "異常", traceback.print_exc())
continue
return movie_data_list
def export_excel(datas):
"""
導(dǎo)出數(shù)據(jù)到Excel
:param datas: 數(shù)據(jù)
:return:
"""
df = pd.DataFrame(datas)
df.to_excel("豆瓣電影TOP250.xlsx")
datas = [] # 所有電影數(shù)據(jù)
for i in range(1, 11): # 遍歷10頁
start = 25 * (i - 1)
url = f"https://movie.douban.com/top250?start={start}&filter="
print(url)
movie_data_list = parse_html(crawl_html(url))
datas += movie_data_list
export_excel(datas)
到了這里,關(guān)于Python爬蟲實戰(zhàn)-批量爬取豆瓣電影排行信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!