bs4的用法之遍歷文件樹以及查詢文件樹

2022-03-03 23:42:51 字數 3506 閱讀 8375

bs4的用法之遍歷文件樹以及查詢文件樹

"""

#### bs4的使用

from bs4 import beautifulsoup

html_doc = """

the dormouse's story

once upon a time there were three little sisters; and their names were

elsie,

lacie and

tillie;

and they lived at the bottom of a well.

..."""

soup = beautifulsoup(html_doc, 'lxml')

# 美化格式

# print(soup.prettify())

##### 遍歷文件樹

#1、用法(通過.來查詢,只能找到第乙個)

# tag物件

head = soup.head

title = head.title

print(head)

print(title)

p = soup.p # 只能找到第乙個

print(p)

# 2、獲取標籤的名稱

# tag物件

p = soup.p

from bs4.element import tag

print(type(p))

print(p.name)

# 3、獲取標籤的屬性

p = soup.p

# 方式一 通過來獲取

# 獲取class屬性,可以有多個,拿到列表

print(p['class'])

print(p['id'])

# 方式二:通過attrs來獲取

print(p.attrs['class'])

print(p.attrs.get('id'))

# 4、獲取標籤的內容

p = soup.p

print(p.text) # 所有層級的都拿出來拼到一起

print(p.string) # 只有一層,可以取出

print(p.strings) # 把每一層都取出來,做成乙個生成器

# 5、巢狀選擇

title = soup.head.title

print(title)

# 6、子節點,子孫節點

p1 = soup.p.children # 迭代器

p2 = soup.p.contents # 列表

print(list(p1))

print(p2)

# 7、父節點,祖先節點

p1 = soup.p.parent # 直接父節點

p2 = soup.p.parents # 祖先節點

print(p1)

print(list(p2)) # 它的祖先,包括它父親的祖先都拿出來

# 8、兄弟節點

print(soup.a.next_sibling) # 下乙個兄弟

print(soup.a.previous_sibling) # 上乙個兄弟

print(list(soup.a.next_siblings)) #下面的兄弟們=>生成器物件

print(soup.a.previous_siblings) #上面的兄弟們=>生成器物件

##### 查詢文件樹(find, find_all) 速度比遍歷文件樹慢

# 可以兩個配合著使用 eg:(soup.p.find())

# 五種過濾器:字串,正規表示式,列表,true,方法

# 這五種下面以find為例

# 1、字串查詢,引號內是字串

p = soup.find(name='p')

p = soup.find(name='body')

print(p)

# 查詢類名是title的所有標籤,由於class是關鍵字,得用class_

ret = soup.find_all(class_='title')

# 查詢屬性為的標籤

ret = soup.find_all(href='')

# 查詢id為xx的標籤

ret = soup.find_all(id='id_p')

print(ret)

# 2、正規表示式

import re

reg = re.compile('^b') #編譯乙個正規表示式,返回乙個物件

ret = soup.find_all(name=reg)

# # 找id以id開頭的標籤

reg = re.compile('^id')

ret = soup.find_all(id=reg)

print(ret)

# 3、列表

ret = soup.find_all(name=['body', 'b'])

ret = soup.find_all(id=['id_p', 'link1'])

ret = soup.find_all(class_=['id_p', 'link1'])

# and 關係

ret = soup.find_all(class_='title', name='p')

print(ret)

# 4、true

# 所有有名字的標籤

ret = soup.find_all(name=true)

# 所有有id的標籤

ret = soup.find_all(id=true)

# 所有有href屬性的

ret = soup.find_all(href=true)

print(ret)

# 5 方法

def has_class_but_no_id(tag):

return tag.has_attr('class') and not tag.has_attr('id')

print(soup.find_all(has_class_but_no_id))

# 6、其他使用

ret = soup.find_all(attrs=)

ret = soup.find_all(attrs=)

print(ret)

# 7、拿到標籤,取屬性,取text

ret = soup.find_all(attrs=)

print(ret[0].text)

# 8、limit(限制條數)

soup.find() # 其實就是find_all 然後limit 為1

ret = soup.find_all(name=true, limit=2)

print(len(ret))

# 9、recursive

# recursive = false # 只找兒子,不遞迴查詢,只找第一層

ret = soup.body.find_all(name='p', recursive=false)

print(ret)

"""

bs4 遍歷文件樹

from bs4 import beautifulsoup html first item second item third item soup beautifulsoup html,lxml ui soup.ui print ui.contents 返回列表 print ui.children ...

bs4之標籤樹的下行遍歷

import requests from bs4 import beautifulsoup def bianlisoup url r requests.get url,timeout 30 r.raise for status demo r.text soup beautifulsoup demo,...

bs4的基本用法

本檔案用來記錄bs4的用法 from bs4 import beautifulsoup 使用方法 將乙個html文件,轉化為指定物件,然後通過物件的方法或屬性去查詢指定的內容 轉化本地檔案 soup beautifulsoup open 本地檔案 lxml 轉化網路檔案 soup beautiful...