BeautifulSoup4-介绍
import requests
from bs4 import BeautifulSoup
# r = requests.get("https://python123.io/ws/demo.html")
# demo = r.text
demo = """
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>
</body></html>
"""
soup = BeautifulSoup(demo, "html.parser")
# 标题
title = soup.title
print(title)
"<title>This is a python demo page</title>"
# <a>标签
tag = soup.a
print(tag.prettify())
‘<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>‘
# 标签类型
print(type(tag))
"<class ‘bs4.element.Tag‘>"
# <a>标签的名字
print(tag.name)
"a"
# <a>的父亲标签的名字
print(tag.parent.name)
"p"
# <a>的父亲的父亲标签的名字
print(tag.parent.parent.name)
"body"
# 标签的属性
print(tag.attrs)
"{‘href‘: ‘http://www.icourse163.org/course/BIT-268001‘, ‘class‘: [‘py1‘], ‘id‘: ‘link1‘}"
# 标签属性值
print(tag.attrs[‘class‘])
"[‘py1‘]"
# 标签属性类型
print(type(tag.attrs))
"<class ‘dict‘>"
# <a>标签的string
print(tag.string)
"Basic Python"
# <p>标签的string
print(soup.p.string)
"The demo python introduces several python courses."
# 标签string的类型
print(type(soup.p.string))
"<class ‘bs4.element.NavigableString‘>"
# 标签的comment
newsoup = BeautifulSoup("<b><!--This is a comment--></b>"
"<p>This is not a comment</p>", "html.parser")
print(newsoup.b.string)
"This is a comment"
print(type(newsoup.b.string))
"<class ‘bs4.element.Comment‘>"
print(newsoup.p.string)
"This is not a comment"
print(type(newsoup.p.string))
"<class ‘bs4.element.NavigableString‘>"
print("------------------------------------------")
# 标签树的下行遍历
# .contents 子节点的列表,将<tag>所有儿子节点存入列表
print(soup.head.contents)
"[<title>This is a python demo page</title>]"
print(soup.body.contents)
# .children 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点
for child1 in soup.body.children:
print(child1)
# .descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历
for child2 in soup.body.descendants:
print(child2)
print("------------------------------------------")
# 标签数的上行遍历
# .parent 节点的父亲标签
print(soup.title.parent)
print(soup.html.parent)
# .parents 节点先辈标签的迭代类型,用于循环遍历先辈节点
for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
print("------------------------------------------")
# 标签数的平行遍历,平行遍历发生在同一个父节点下的各节点间
# .next_sibling 返回按照HTML文本顺序的下一个平行节点标签
print(soup.a.next_sibling)
" and "
print(soup.a.next_sibling.next_sibling)
# .previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)
# .next_siblings 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
for sibling in soup.a.next_siblings:
print(sibling)
# .previous_siblings 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签
for sibling in soup.a.previous_siblings:
print(sibling)