天天看點

【爬蟲系列】爬取小說網站--Bs4,nginx面試題騰訊

【爬蟲系列】爬取小說網站--Bs4,nginx面試題騰訊
在pip檔案夾裡面建立一個檔案叫做  pip.ini ,内容寫如下即可

 [global]

 timeout = 6000

 index-url = https://mirrors.aliyun.com/pypi/simple/

 trusted-host = mirrors.aliyun.com

 linux

(1)cd ~ 

(2)mkdir ~/.pip

(3)vi ~/.pip/pip.conf

(4)編輯内容,和windows一模一樣

- 需要安裝:

- pip install bs4

- pip install lxml



           
【爬蟲系列】爬取小說網站--Bs4,nginx面試題騰訊

2、Bs4進行資料解析

1 資料解析的原理:

# -1.标簽定位

# -2.提取标簽、标簽屬性中存儲的資料值



           

2 bs4資料解析的原理:

# -1.執行個體化一個BeautifulSoup對象,并且将頁面源碼資料加載到該對象中

# -2.通過調用BeautifulSoup對象中相關的屬性或者方法進行标簽定位和資料提取



           

3 環境安裝:

# - pip install bs4

# - pip install lxml



           

3、如何執行個體化BeautifulSoup對象:

# from bs4 import BeautifulSoup

    # 對象的執行個體化:

        # - 1.将本地的html文檔中的資料加載到該對象中

        # fp = open( './test.html' ,'r' ,encoding='utf-8')soup =BeautifulSoup(fp, 'lxml' )

        # -2.将網際網路上擷取的頁面源碼加載到該對象中

        # page_text =response.text

        # soup =BeatifulSoup(page_text, ' lxml')



           

4、提供的用于資料解析的方法和屬性:

# - soup.tagName:傳回的是文檔中第一次出現的tagName對應的标簽- soup.find( ):

# -find( 'tagName '):等同于soup.div一屬性定位:

# -soup.find( 'div' ,class_/id/attr= 'song ')

# - soup.find_all( 'tagName '):傳回符合要求的所有标簽(清單)select:

# - select('某種選擇器(id,class,标簽...選擇器)‘),傳回的是一個清單。一層級選擇器:

# - soup.select( '.tang > ul > li > a'):>表示的是一個層級- oup.select( '.tang > ul a'):空格表示的多個層級



           

5、擷取标簽之間的文本資料:

# - soup.a.text/stringlget_text()

# - text/get_text():可以擷取某一個标簽中所有的文本内容- string:隻可以擷取該标簽下面直系的文本内容



           
下一篇: BS4的學習