基於bs4庫的HTML查詢方法

2021-10-02 13:03:46 字數 1331 閱讀 4374

<>.find_all(name,attrs,recursive,string,**kwargs)

返回乙個列表型別,內部儲存查詢的結果

對標籤名稱的檢索字串

import requests

from bs4 import beautifulsoup

r=requests.get('')

demo=r.text

soup=beautifulsoup(demo,'html.parser')

list_a=soup.find_all('a')# 查詢多個標籤

for item in list_a:

print(item)

list_a_b=soup.find_all(['a','b'])#查詢多個標籤

print("------------")

for item in list_a_b:

print(item)

# 查詢所有的標籤

for tag in soup.find_all(true):

print(tag.name)

對標籤屬性值檢索的字串,可標註屬性檢索

# 查詢對應屬性的標籤

print(soup.find_all('p','course'))

#查詢對乙個id的資訊

print(soup.find_all(id='link1'))

是否針對子孫節點全部進行搜尋,預設是true

soup.find_all('a',recursive=false)#不對子孫節點進行搜尋
(...)等價於.find_all()

soup(...)等價於soup.find_all()

方法說明

<>.find()

搜尋僅僅返回乙個結果,字串型別,同find_all引數

<>.find_parents()

在先輩中搜尋,返回列表型別,同find_all引數

<>.find_parent()

在先輩節點中返回乙個結果,字串型別,同find引數

<>.find_next_siblings()

在後續平行節點中搜尋,返回列表型別,同find引數

<>.find_next_sibling()

在後續平行節點返回乙個結果,字串型別,同find引數

<>.find_previous_siblings()

在前序平行節點中搜尋,返回列表型別,同find引數

<>.find_previous_sibling()

在前序平行節點返回乙個結果,字串型別,同find引數

基於bs4庫的HTML標籤遍歷方法

html可以看做一棵標籤樹 屬性說明 contents 將該標籤所有的兒子節點存入列表 children 子節點的迭代型別,和contents類似,用於遍歷兒子節點 descendants 子孫節點的迭代型別,包含所有的子孫跌點,用於迴圈遍歷 import requests from bs4 imp...

基於bs4庫的HTML內容遍歷方法

html基本格式 樹型格式 遍歷方式 下行遍歷 根節點到葉節點 上行遍歷 葉節點到根節點 平行遍歷 標籤樹的下行遍歷 屬性說明 contents 子節點的列表,將所有兒子節點存入列表 children 子節點的迭代型別,與.contents類似,用於迴圈遍歷兒子節點 descendants 子孫節點...

基於bs4的網頁遊歷

1.html的基本格式 1.下行遊歷。1.1 contents import requests r requests.get demo r.text from bs4 import beautifulsoup soup beautifulsoup demo,html.parser print sou...