beautifulsoup4
靈活又方便的網頁解析庫,處理高效,支持多種解析器。利用它不用編寫正則表達式即可方便地實現網頁的提取。
使用方法:
案例代碼展示可不看
import requests
from bs4 import *
import re
urls='https://www.dbmeinv.com/' #這是一個urls地址
res=requests.get(urls) #獲取網頁內容
t=res.text #網頁 內容以文本形式賦值給t
#匹配圖片地址的正則
# needs=r'https://wx.*?jpg'
# pattern = re.compile(needs)
# need= pattern.findall(t)
#獲取標簽內容和圖片的地址的代碼
soup = BeautifulSoup(t,'lxml') # 解析器為lxml
list_img=soup.find('img') #尋找所有的img 標簽
print(type(list_img[0])) #獲取src屬性即 圖片的唯一地址
print(list_img[0].get('src'))
for img_src in list_img:
print(img_src.get('src')) #遍歷圖片的地址
list_url=soup.find_all('a') #尋找所有的a標簽
print(type(list_url[0]))
print(list_url[0].get('href'))
for img_url in list_img:
print(img_url.get_text()) #獲取a標簽中的文本內容
soup=BeautifulSoup(t,'html.parser')
print(soup.find_all('a',class_='sister'))#class是特殊屬性,因為class是關鍵字
print(soup.find_all('a',id='link3'))#用於查找有特定屬性的標簽
sibling_soup = BeautifulSoup("<a><b id='bbb'>text1</b><b>text2</b></b></a>", 'html.parser')
print(sibling_soup)
print(sibling_soup.find('b',id='bbb')) #獲取id為bbb的標簽
print(sibling_soup.find('b',id='bbb').next_sibling) #獲取id為bbb的標簽的下一個b標簽
****
使用BeatuifulSoup
引用 BeatuifulSoup
最常用的引用方式
Ps:B S需要大寫
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(“<p>data</p>” , ‘html.parser’)
#兩個參數 一個是需要解析的內容 一個是解析器
Soup=BeautifulSoup( open(path,”html.parser”))
BeatuifulSoup 基本元素
標簽的認識可以參考html
屬性的應用
Demo是一個respose.text
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.title)
#打印出頁面的title 包含閉合標簽的內容
Tag=soup.a
Print(Tag)#打印a標簽的內容包括各種屬性
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Soup.a.name #獲取a標簽的名字
Soup.a.parent.name #霸霸
Soup.a.parent.parent.name #霸霸的霸霸
#對於一個標簽可以使用.name 的方式獲得他的名字 返回的是一個字符串形式
Tag=soup.a
Print(Tag.attrs)
#返回標簽的屬性 是一個字典格式
#對信息的提取
Tag.attrs[‘class’] #獲取class的值
Tag.attrs[“href”]# 獲取href 屬性
#Tag的類型 是一個bs4種的一個內置類型
Soup.a.string #a標簽內容的屬性
#可以跨越多個標簽層次即:無視標簽內容含有的標簽
當標簽內容中含有注釋的時候
Soup.b.string 會正常打印標簽內容無視注釋效果
可通過類型判斷
Print(type(“Soup.b.string”))
Print(type(“Soup.b.string”))
#基於bs4庫中的htm庫的遍歷方法
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.head)獲取head標簽
print(Soup.head.contents) #head標簽中的兒子標簽是title標簽
#返回的內容是一個列表
print(Soup.body.contents)#獲取body的兒子標簽
Print(len(Soup.body.contents)) #獲取body標簽的兒子標簽的數量
Ps:子節點不知包括標簽 甚至一些’\n’ 之類的換行符都可以是子節點
可通過列表類型的下表來檢索其中的內容
Soup.body.contents[1] #獲取第二個元素
#也可以通過for循環遍歷
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.title.parent) #打印出title標簽的霸霸
print(Soup.html.parent)#html 標簽是doc中的最高級標簽他的霸霸是他自己包含其中自己的全部內容
print(Soup.parent)#soup本身沒有霸霸
這里插入一個方法
Soup.prettify()
#這個方法就是在每個標簽后加入一個\n
打印出來是十分規范的h5代碼 一目了然
也可以對某個標簽做格式化處理
Print(Soup.a.prettify())
會發現標簽內容被漂亮的打印出來
標簽樹的平行遍歷是有條件的
Ps:平行遍歷發生在同一父節點的各節點間
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.a.next_sibling) #打印出a標簽的下一個平行標簽
#這里打印出一個’and’類型於之前提到過的’\n’
Print(Soup.a.next_sibling.next_sibling)#打印出a標簽的下一個平行標簽的下一個平行標簽
print(Soup.a.previous)#打印出的是一段文本解釋a標簽之前的信息
#同時也可以打印出a標簽的前前平行標簽的信息
print(Soup.a.previous.previous)
find_all方法
Soup.find_all(“a”) #查找所有的a標簽
#同時查找a,b標簽
Soup.find_all([“a”,”b”])#作為一個列表形式
for tag in Soup.find_all(True):
Print(tag.name)
會打印出所有的標簽
Soup.find_all(“p”,”course”) #返回帶有 course屬性值的所有p標簽 (列表)
Soup.find_all(id=link1) #查找id屬性為link1的標簽元素 (列表)
如果沒有會返回一個空列表
beautifulsoup4
靈活又方便的網頁解析庫,處理高效,支持多種解析器。利用它不用編寫正則表達式即可方便地實現網頁的提取。
使用方法:
案例代碼展示可不看
import requests
from bs4 import *
import re
urls='https://www.dbmeinv.com/' #這是一個urls地址
res=requests.get(urls) #獲取網頁內容
t=res.text #網頁 內容以文本形式賦值給t
#匹配圖片地址的正則
# needs=r'https://wx.*?jpg'
# pattern = re.compile(needs)
# need= pattern.findall(t)
#獲取標簽內容和圖片的地址的代碼
soup = BeautifulSoup(t,'lxml') # 解析器為lxml
list_img=soup.find('img') #尋找所有的img 標簽
print(type(list_img[0])) #獲取src屬性即 圖片的唯一地址
print(list_img[0].get('src'))
for img_src in list_img:
print(img_src.get('src')) #遍歷圖片的地址
list_url=soup.find_all('a') #尋找所有的a標簽
print(type(list_url[0]))
print(list_url[0].get('href'))
for img_url in list_img:
print(img_url.get_text()) #獲取a標簽中的文本內容
soup=BeautifulSoup(t,'html.parser')
print(soup.find_all('a',class_='sister'))#class是特殊屬性,因為class是關鍵字
print(soup.find_all('a',id='link3'))#用於查找有特定屬性的標簽
sibling_soup = BeautifulSoup("<a><b id='bbb'>text1</b><b>text2</b></b></a>", 'html.parser')
print(sibling_soup)
print(sibling_soup.find('b',id='bbb')) #獲取id為bbb的標簽
print(sibling_soup.find('b',id='bbb').next_sibling) #獲取id為bbb的標簽的下一個b標簽
****
使用BeatuifulSoup
引用 BeatuifulSoup
最常用的引用方式
Ps:B S需要大寫
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(“<p>data</p>” , ‘html.parser’)
#兩個參數 一個是需要解析的內容 一個是解析器
Soup=BeautifulSoup( open(path,”html.parser”))
BeatuifulSoup 基本元素
標簽的認識可以參考html
屬性的應用
Demo是一個respose.text
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.title)
#打印出頁面的title 包含閉合標簽的內容
Tag=soup.a
Print(Tag)#打印a標簽的內容包括各種屬性
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Soup.a.name #獲取a標簽的名字
Soup.a.parent.name #霸霸
Soup.a.parent.parent.name #霸霸的霸霸
#對於一個標簽可以使用.name 的方式獲得他的名字 返回的是一個字符串形式
Tag=soup.a
Print(Tag.attrs)
#返回標簽的屬性 是一個字典格式
#對信息的提取
Tag.attrs[‘class’] #獲取class的值
Tag.attrs[“href”]# 獲取href 屬性
#Tag的類型 是一個bs4種的一個內置類型
Soup.a.string #a標簽內容的屬性
#可以跨越多個標簽層次即:無視標簽內容含有的標簽
當標簽內容中含有注釋的時候
Soup.b.string 會正常打印標簽內容無視注釋效果
可通過類型判斷
Print(type(“Soup.b.string”))
Print(type(“Soup.b.string”))
#基於bs4庫中的htm庫的遍歷方法
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.head)獲取head標簽
print(Soup.head.contents) #head標簽中的兒子標簽是title標簽
#返回的內容是一個列表
print(Soup.body.contents)#獲取body的兒子標簽
Print(len(Soup.body.contents)) #獲取body標簽的兒子標簽的數量
Ps:子節點不知包括標簽 甚至一些’\n’ 之類的換行符都可以是子節點
可通過列表類型的下表來檢索其中的內容
Soup.body.contents[1] #獲取第二個元素
#也可以通過for循環遍歷
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.title.parent) #打印出title標簽的霸霸
print(Soup.html.parent)#html 標簽是doc中的最高級標簽他的霸霸是他自己包含其中自己的全部內容
print(Soup.parent)#soup本身沒有霸霸
這里插入一個方法
Soup.prettify()
#這個方法就是在每個標簽后加入一個\n
打印出來是十分規范的h5代碼 一目了然
也可以對某個標簽做格式化處理
Print(Soup.a.prettify())
會發現標簽內容被漂亮的打印出來
標簽樹的平行遍歷是有條件的
Ps:平行遍歷發生在同一父節點的各節點間
from bs4 import BeatuifulSoup
Soup = BeatuifulSoup(demo,””html.parser)
Print(Soup.a.next_sibling) #打印出a標簽的下一個平行標簽
#這里打印出一個’and’類型於之前提到過的’\n’
Print(Soup.a.next_sibling.next_sibling)#打印出a標簽的下一個平行標簽的下一個平行標簽
print(Soup.a.previous)#打印出的是一段文本解釋a標簽之前的信息
#同時也可以打印出a標簽的前前平行標簽的信息
print(Soup.a.previous.previous)
find_all方法
Soup.find_all(“a”) #查找所有的a標簽
#同時查找a,b標簽
Soup.find_all([“a”,”b”])#作為一個列表形式
for tag in Soup.find_all(True):
Print(tag.name)
會打印出所有的標簽
Soup.find_all(“p”,”course”) #返回帶有 course屬性值的所有p標簽 (列表)
Soup.find_all(id=link1) #查找id屬性為link1的標簽元素 (列表)
如果沒有會返回一個空列表