pythonbeautifulsoup库⼊门安装教程
⽬录
beautiful soup库的安装
beautiful soup库的理解
beautiful soup库的引⽤
BeautifulSoup类
回顾demo.html
Tag标签
Tag的attrs(属性)
Tag的NavigableString
HTML基本格式
标签树的下⾏遍历
标签树的上⾏遍历
标签的平⾏遍历
bs库的prettify()⽅法
bs4库的编码
beautiful soup库的安装
pip install beautifulsoup4
beautiful soup库的理解
beautiful soup库是解析、遍历、维护“标签树”的功能库
beautiful soup库的引⽤
from bs4 import BeautifulSoup
import bs4
BeautifulSoup类
BeautifulSoup对应⼀个HTML/XML⽂档的全部内容
回顾demo.html
import requests
r = ("python123.io/ws/demo.html")
demo = r.text
print(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="/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p> </body></html>
Tag标签
基本元素说明
Tag标签,最基本的信息组织单元,分别⽤<>和</>标明开头和结尾
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
java invoke用法demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.title)
tag = soup.a
print(tag)
<title>This is a python demo page</title>
<a  href="/course/BIT-268001" >Basic Python</a>
任何存在于HTML语法中的标签都可以⽤soup.访问获得。当HTML⽂档中存在多个相同对应内容时,soup.返回第⼀个
Tag的name
基本元素说明
Name 标签的名字,
的名字是'p',格式:.name
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html") demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.a.name)
print(soup.a.parent.name)
print(soup.a.parent.parent.name)
a
p
body
Tag的attrs(属性)
基本元素说明
Attributes标签的属性,字典形式组织,格式:.attrs
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
tag = soup.a
print(tag.attrs)
print(tag.attrs['class'])
print(tag.attrs['href'])
print(type(tag.attrs))
print(type(tag))
{'href': '/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
/course/BIT-268001
<class 'dict'>
<class 'bs4.element.Tag'>
Tag的NavigableString
Tag的NavigableString
基本元素说明
NavigableString标签内⾮属性字符串,<>…</>中字符串,格式:.string
Tag的Comment
基本元素说明
Comment标签内字符串的注释部分,⼀种特殊的Comment类型
import requests
from bs4 import BeautifulSoup
newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>","html.parser")
print(newsoup.b.string)
print(type(newsoup.b.string))
print(newsoup.p.string)
print(type(newsoup.p.string))
This is a comment
<class 'bs4.element.Comment'>
This is not a comment
<class 'bs4.element.NavigableString'>
HTML基本格式
标签树的下⾏遍历
属性说明
.contents⼦节点的列表,将所有⼉⼦结点存⼊列表
.children⼦节点的迭代类型,与.contents类似,⽤于循环遍历⼉⼦结点
.descendents⼦孙节点的迭代类型,包含所有⼦孙节点,⽤于循环遍历
论坛开源模板
BeautifulSoup类型是标签树的根节点
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.head)
print(ts)
print(ts)
print(len(ts))
print(ts[1])
springboot控制器注解<head><title>This is a python demo page</title></head>
[<title>This is a python demo page</title>]
['\n', <p ><b>The demo python introduces several python courses.</b></p>, '\n', <p >Python
is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the
following courses:
<a  href="/course/BIT-268001" >Basic Python</a> and <a  href="/course/BIT-1001870001" >Advanced Python</a>.</p>, '\n'] 5
<p ><b>The demo python introduces several python courses.</b></p>
for child in soup.body.children:
print(child)  #遍历⼉⼦结点
for child in soup.body.descendants:
print(child) #遍历⼦孙节点
标签树的上⾏遍历
属性说明
.parent节点的⽗亲标签
.parents节点先辈标签的迭代类型,⽤于循环遍历先辈节点
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.title.parent)
print(soup.html.parent)
<head><title>This is a python demo page</title></head>
<html><head><title>This is a python demo page</title></head>
<body>
<p ><b>The demo python introduces several python courses.</b></p>
正则表达式 中文字符<p >Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a  href="/course/BIT-268001" >Basic Python</a> and <a  href="/course/BIT-1001870001" >Advanced Python</a>.</p>
</body></html>
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
p
body
html
[document]
标签的平⾏遍历
属性说明
.next_sibling返回按照HTML⽂本顺序的下⼀个平⾏节点标签
.previous.sibling返回按照HTML⽂本顺序的上⼀个平⾏节点标签
.next_siblings迭代类型,返回按照HTML⽂本顺序的后续所有平⾏节点标签
.previous.siblings迭代类型,返回按照HTML⽂本顺序的前续所有平⾏节点标签
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(_sibling)
print(__sibling)
print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)
print(soup.a.parent)
and
<a class="py2" href="/course/BIT-1001870001" id="link2">Advanced Python</a>
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
None
<p class="course">Python is a wonderful general-purpose programming language. You can learn Pyt
hon from novice to professional by tracking the following courses:
<a class="py1" href="/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="/course/BIT-1001870001" id="link2">Advanced Python</a>.</p> for sibling in _sibling:
print(sibling)  #遍历后续节点
for sibling in soup.a.previous_sibling:
print(sibling)  #遍历前续节点
bs库的prettify()⽅法
import requests
from bs4 import BeautifulSoup
r = ("python123.io/ws/demo.html")
数据库的建立代码demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.prettify())
<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:
Basic Python
</a>
and
<a class="py2" href="/course/BIT-1001870001" id="link2">
Advanced Python
</a>
.
</p>python入门教程(非常详细)书
</body>
</html>
.prettify()为HTML⽂本<>及其内容增加更加'\n'
.prettify()可⽤于标签,⽅法:.prettify()
bs4库的编码
bs4库将任何HTML输⼊都变成utf-8编码
python 3.x默认⽀持编码是utf-8,解析⽆障碍
import requests
from bs4 import BeautifulSoup
soup = BeautifulSoup("<p>中⽂</p>","html.parser")
print(soup.p.string)
print(soup.p.prettify())
中⽂
<p>
中⽂
</p>
到此这篇关于python beautiful soup库⼊门安装教程的⽂章就介绍到这了,更多相关python beautiful soup库⼊门内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。