輕松掌握Python爬蟲,從入門到精通
一、什么是爬蟲
網(wǎng)絡(luò)爬蟲,是一種按照一定規(guī)則,自動抓取互聯(lián)網(wǎng)信息的程序或者腳本。由于互聯(lián)網(wǎng)數(shù)據(jù)的多樣性和資源的有限性,根據(jù)用戶需求定向抓取相關(guān)網(wǎng)頁并分析也成為如今主流的爬取策略。
1 爬蟲可以做什么
你可以爬取網(wǎng)絡(luò)上的的圖片,爬取自己想看的視頻等等,只要你能通過瀏覽器訪問的數(shù)據(jù)都可以通過爬蟲獲取。
2 爬蟲的本質(zhì)是什么
模擬瀏覽器打開網(wǎng)頁,獲取網(wǎng)頁中我們想要的那部分?jǐn)?shù)據(jù)
學(xué)習(xí)案例:爬取豆瓣電影 Top 250的基本信息
引入第三方模塊
# 引入第三方模塊
from bs4 import BeautifulSoup #網(wǎng)頁解析,獲取數(shù)據(jù)
import re #正則表達式,進行文字匹配
import urllib.request,urllib.error #制定URL,獲取網(wǎng)頁數(shù)據(jù)
import xlwt #進行Excel操作
import sqlite3 #進行數(shù)據(jù)庫操作
正則表達式——制定獲取數(shù)據(jù)規(guī)則
# 影片詳情鏈接規(guī)則
findLink=re.compile(r'<a href="(.*?)">') #創(chuàng)建正則表達式對象,表示規(guī)則(字符串模式)
findImg=re.compile(r'<img.*src="(.*?)?"',re.S) #影片圖片
findTitle=re.compile(r'<span class="title">(.*)</span>') #影片名字
findRating=re.compile(r'<span class="rating_num" property="v:average">(.*)</span>') #影片評分
findJudge=re.compile(r'<span>(\d*)人評價</span>') #影片評價人數(shù)
findIng=re.compile(r'<span class="ing">(.*)</span>') #找到概況
findBd=re.compile(r'<p class="">(.*?)</p>',re.S) #找到影片的相關(guān)內(nèi)容
完整代碼
# 引入第三方模塊 from bs4 import BeautifulSoup #網(wǎng)頁解析,獲取數(shù)據(jù) import re #正則表達式,進行文字匹配 import urllib.request,urllib.error #制定URL,獲取網(wǎng)頁數(shù)據(jù) import xlwt #進行Excel操作 import sqlite3 #進行數(shù)據(jù)庫操作 def main(): baseurl="https://movie.douban.com/top250?start=" dataList=getData(baseurl) savepath=".\\豆瓣電影Top250.xls" # 影片詳情鏈接規(guī)則 findLink=re.compile(r'<a href="(.*?)">') #創(chuàng)建正則表達式對象,表示規(guī)則(字符串模式) findImg=re.compile(r'<img.*src="(.*?)?"',re.S) #影片圖片 findTitle=re.compile(r'<span class="title">(.*)</span>') #影片名字 findRating=re.compile(r'<span class="rating_num" property="v:average">(.*)</span>') #影片評分 findJudge=re.compile(r'<span>(\d*)人評價</span>') #影片評價人數(shù) findIng=re.compile(r'<span class="ing">(.*)</span>') #找到概況 findBd=re.compile(r'<p class="">(.*?)</p>',re.S) #找到影片的相關(guān)內(nèi)容 def getData(baseurl): dataList=[] for i in range(0,10):# 調(diào)用獲取頁面信息的函數(shù) 10次 url=baseurl+ str(i*25) html=askURL(url) # 保存獲取到的源碼 soup=BeautifulSoup(html,"html.parser") for item in soup.find_all('div',class_="item"): #查找符合要求的字符串 data=[] #保存電影信息 item =str(item) #獲取影片詳情鏈接 link=re.findall(findLink,item)[0] #re庫用來通過正則表達式查找指定的字符串 data.append(link) #添加鏈接 imgSrc=re.findall(findImg,item)[0] data.append(imgSrc) #添加圖片 titles=re.findall(findTitle, item)[0] if(len(titles)==2): #片名可能有兩個名字,一個中文,一個外文 ctitle=titles[0] data.append(ctitle) #添加中文名 otitle=titles[1].replace("/","") data.append(otitle) #添加外文名 else: data.append(titles[0]) # 添加圖片 data.append(' ') #留空,保持Excel數(shù)據(jù)一致性 rating=re.findall(findRating, item)[0] data.append(rating) # 添加評分 judgeNum = re.findall(findJudge, item)[0] data.append(judgeNum) # 添加評分人數(shù) ing = re.findall(findIng, item) if len(ing)!=0: ing=ing[0].replace("。","") data.append(ing) # 添加概述 else: data.append(" ") bd = re.findall(findBd, item)[0] bd=re.sub('<br(\s+)?/>(\s+)?'," ",bd) bd=re.sub('/'," ",bd) data.append(bd.strip()) # 添加影片的相關(guān)內(nèi)容 dataList.append(data) #把處理好的電影信息放入dataList print(dataList) return dataList # 得到指定一個URL的網(wǎng)頁內(nèi)容 def askURL(url): # head用戶代理,本質(zhì)上告訴瀏覽器我們接收什么水平的數(shù)據(jù) head={ "User-Agent": "Mozilla / 5.0(Linux;Android6.0;Nexus5 Build / MRA58N) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 80.0.3987.87 Mobile Safari / 537.36" } request=urllib.request.Request(url,headers=head) html="" try: response=urllib.request.urlopen(request) html=response.read().decode("utf-8") # print(html) except urllib.error.URLError as e: if hasattr(e,"code"): print(e.code) if hasattr(e,"reason"): print(e.reason) return html def saveData(savepath): print("save.....") if __name__ =="__main__": # 調(diào)用函數(shù) main()
到此這篇關(guān)于輕松掌握Python爬蟲,從入門到精通的文章就介紹到這了,更多相關(guān)Python爬蟲學(xué)習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Matplotlib繪制折線圖、散點圖、柱狀圖、直方圖、餅圖的實例
這篇文章主要介紹了利用Matplotlib繪制折線圖、散點圖、柱狀圖、直方圖、餅圖的實例代碼,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09在django中,關(guān)于session的通用設(shè)置方法
今天小編就為大家分享一篇在django中,關(guān)于session的通用設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作,涉及Python正則爬取數(shù)據(jù)及針對mysql數(shù)據(jù)庫的存儲操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-06-06在django中實現(xiàn)choices字段獲取對應(yīng)字段值
這篇文章主要介紹了在django中實現(xiàn)choices字段獲取對應(yīng)字段值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python基于jieba, wordcloud庫生成中文詞云
這篇文章主要介紹了Python基于jieba, wordcloud庫生成中文詞云,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05python2 與 pyhton3的輸入語句寫法小結(jié)
這篇文章主要給大家介紹了關(guān)于python2 與 pyhton3的輸入語句寫法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09