python爬蟲 BeautifulSoup庫使用

2021-10-04 09:24:26 字數 4163 閱讀 5411

概述: beautifulsoup是乙個靈活方便的解析庫,處理高效,支援多種解析器,利用它不用編寫正規表示式即可方便實現網頁資訊的提取

解析器使用方法

優勢劣勢

python標準庫

beautifulsoup(markup, 「html.parser」)

python的內建標準庫執行速度適中文件容錯能力強 python 2.7.3 or 3.2.2)前 的版本中文件

容錯能力差

lxml html 解析器

beautifulsoup(markup, 「lxml」)

速度快文件容錯能力強

需要安裝c語言庫

lxml xml 解析器

beautifulsoup(markup, 「xml」

速度快唯一支援xml的解析器

需要安裝c語言庫

html5lib

beautifulsoup(markup, 「html5lib」)

最好的容錯性以瀏覽器的方式解析文件生成html5格式的文件

速度慢不依賴外部擴充套件

soup.prettify() 格式化輸出

soup.title.sting title標籤內容字串輸出

soup.title.text/get_text() title標籤內容字串輸出

選擇元素

html =

"""hello python

"""from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.title)

# 如果多個,輸出第乙個結果

print

(type

(soup.head)

)print

(soup.p)

輸出:demo<

/title>

<

class

'bs4.element.tag'

>

hello python<

/p>

獲取名稱

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.title.name)

# 獲取標籤名

輸出:title

獲取屬性

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.p[

'name'])

# 獲取屬性名

獲取內容

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.p.string)

# 獲取標籤內容

print

(soup.p.text)

# 獲取標籤內容

子節點和子孫節點

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.body.contents)

# 用類表的形式返回子節點

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.body.children)

for i,child in

enumerate

(soup.body.children)

:# 返回索引和子節點

print

(i,child)

父節點和祖先節點

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.span.parent)

# 獲取父節點

print

(list

(enumerate

(soup.span.parents)))

# 獲取祖先節點

兄弟節點

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(list

(enumerate

(soup.p.next_siblings)))

# 獲取後面的兄弟節點

print

(list

(enumerate

(soup.previous_siblings)))

# 獲取前面的兄弟節點

find_all(name,attrs,recursive,text,**kwargs) 可根據標籤名,屬性,內容查詢文件(返回所有匹配結果)

name

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)# print(soup.find_all('p'))

# print(soup.find_all('p')[0])

for p in soup.find_all(

'p')

:print

(p.find_all(

'span'))

print

(p.find_all(

'span')[

0].text)

attrs

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.find_all(attrs=))

print

(soup.find_all(class_=

'test'))

# class和id有特殊方法

print

(soup.find_all(id=

'span'

))

text

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.find_all(text=

'123'))

# 只是返回匹配的內容,不返回標籤

find(name,attrs,recursive,text,**kwargs) 可根據標籤名,屬性,內容查詢文件(返回第乙個匹配結果)

select()直接傳入css選擇器即可

from bs4 import beautifulsoup

soup = beautifulsoup(html,

'lxml'

)print

(soup.select(

'.test.test2 .test4'))

# 類print

(soup.select(

'a .test3'))

# 巢狀

print

(soup.select(

'#span'))

#idprint

(soup.select(

'p[name]'))

# 屬性

print

(soup.select(

'p[name=test]'))

```

python爬蟲 非同步爬蟲

壞處 無法無限制的開啟多執行緒或者多程序。執行緒池 程序池 適當使用 使用非同步實現高效能的資料爬取操作 人多力量大 環境安裝 pip install aiohttp 使用該模組中的clientsession 2表示同時存在兩個協程 pool pool 2 urls for i in range 1...

Python爬蟲 初識爬蟲

模擬瀏覽器開啟網頁,獲取網頁中我們想要的那部分資料 瀏覽器開啟網頁的過程 當你在瀏覽器中輸入位址後,經過dns伺服器找到伺服器主機,向伺服器傳送乙個請求,伺服器經過解析後傳送給使用者瀏覽器結果,包括html,js,css等檔案內容,瀏覽器解析出來最後呈現給使用者在瀏覽器上看到的結果 瀏覽器傳送訊息給...

python爬蟲基本流程 Python爬蟲流程

python爬蟲流程 主要分為三個部分 1 獲取網頁 2 解析網頁 獲取資料 儲存資料 三個流程的技術實現 1.獲取網頁 獲取網頁的技術基礎 urllib requests selenium 獲取網頁的高階技術 多執行緒抓取 登入抓取 突破ip限制和伺服器抓取 2.解析網頁 解析網頁的技術基礎 re...