目录
- 🌟Beautiful Soup库入门
- (1) 🍉标签基本元素
- (2) 🍉标签树下的下行遍历
- (3) 🍉标签树的上行遍历
- (4) 🍉标签树的平衡遍历
🌟Beautiful Soup库入门
(1) 🍉标签基本元素
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾 |
Name | 标签的名字, …… 的名字是‘p’,格式:.name |
Attributes | 标签的属性,字典形式组织,格式:.attrs |
NavigableString | 标签内非属性字符串,<>……</>中字符串,格式:.string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |
-
最基本的信息单元
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" try:response=requests.get(url)response.raise_for_status()html=response.textsoup=BeautifulSoup(html,"html.parser")title=soup.titlea=soup.aprint(title,a) except:print("爬取失败")
-
标签的名字
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" try:response=requests.get(url)response.raise_for_status()html=response.textsoup=BeautifulSoup(html,"html.parser")title=soup.title.name # 获取标签的尖括号里面的内容print(title) except:print("爬取失败")
-
标签属性
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" try:response=requests.get(url)response.raise_for_status()html=response.textsoup=BeautifulSoup(html,"html.parser")a=soup.aid=a.attrs # 查看a标签里的属性id_class=a.attrs['href'] # 获取属性的键值对,就是属性内容print(id_class) except:print("爬取失败")
-
标签内非属性字符串
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" try:response=requests.get(url)response.raise_for_status()html=response.textsoup=BeautifulSoup(html,"html.parser")p=soup.p.stringprint(p) except:print("爬取失败")
-
标签内字符串的注释部分
from bs4 import BeautifulSoupsoup=BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>","html.parser")print(type(soup.b.string)) except:print("爬取失败")
(2) 🍉标签树下的下行遍历
属性 | 说明 |
---|---|
.contents | 子节点的列表,将所有儿子节点存入列表 |
.children | 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点 |
.descendants | 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 |
-
遍历下行
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" response=requests.get(url) soup=BeautifulSoup(response.text,"html.parser") head=soup.head.contents body=soup.body.contents print(head) print(body)
-
遍历下行
import requests from bs4 import BeautifulSoup url="http://python123.io/ws/demo.html" response=requests.get(url) soup=BeautifulSoup(response.text,"html.parser") # 遍历儿子节点 for child in soup.body.children:print(child) # 遍历子孙节点 for child in soup.body.descendants:print(child)
(3) 🍉标签树的上行遍历
属性 | 说明 |
---|---|
.parent | 节点的父亲标签 |
.parents | 节点先辈标签的迭代类型,用于循环遍历辈点 |
import requests
from bs4 import BeautifulSoup
url="http://python123.io/ws/demo.html"
response=requests.get(url)
soup=BeautifulSoup(response.text,"html.parser")
title=soup.title.parent
print(title)
for parent in soup.a.parents:if parent is None:print(parent)else:print(parent.name)
(4) 🍉标签树的平衡遍历
属性 | 说明 |
---|---|
.next_sibling | 返回按照HTML文本顺序的下一个平行节点标签 |
.previous_sibling | 返回按照HTML文本顺序的上一个平行节点标签 |
.next_siblings | 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签 |
.previous_siblings | 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签 |
import requests
from bs4 import BeautifulSoup
url="http://python123.io/ws/demo.html"
response=requests.get(url)
soup=BeautifulSoup(response.text,"html.parser")
a0=soup.a.next_sibling
print(a0)
a1=soup.a.next_sibling.next_sibling
print(a1)
a2=soup.a.previous_sibling
print(a2)
a3=soup.a.previous_sibling.previous_sibling
print(a3)
a4=soup.a.parent
print(a4)
# 遍历后续节点
for sibling in soup.a.next_sibling:print(sibling)
# 遍历前续节点
for sibling in soup.a.previous_sibling:print(sibling)
-
.prettify()为HTML文本<>及其内容增加更加”\n“
-
.prettofuy()可用与标签,方法:.prettift()
import requests
from bs4 import BeautifulSoup
url="http://python123.io/ws/demo.html"
response=requests.get(url)
soup=BeautifulSoup(response.text,"html.parser")
print(soup.prettify())