欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

輕松掌握Python爬蟲,從入門到精通

 更新時間:2024年03月27日 14:38:22   作者:COMEGy  
Python爬蟲學(xué)習(xí)完整版來了!想成為一名爬蟲高手,掌握數(shù)據(jù)采集的技能嗎?這份指南將帶你從零開始,一步步掌握Python爬蟲的各種技巧,讓你輕松獲取海量數(shù)據(jù),需要的朋友可以參考下

一、什么是爬蟲

網(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)文章

最新評論