前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python: BeautifulSoup库入门

Python: BeautifulSoup库入门

作者头像
Exploring
发布2022-09-20 13:58:38
2920
发布2022-09-20 13:58:38
举报
文章被收录于专栏:数据处理与编程实践

文章背景:进行网络爬虫时,通过Requests模块获取网页的全部内容,借助BeautifulSoup模块从网页中提取内容。本文对BeautifulSoup模块的使用进行简单的介绍。

例子中用到的HTML页面地址:https://python123.io/ws/demo.html

代码语言:javascript
复制
<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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
    Basic Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

标签树:

BeautifulSoup库是解析、遍历、维护标签树的功能库。

1 BeautifulSoup库的解析器2 BeautifulSoup类的基本元素3 基于bs4库的HTML内容遍历方法3.1 标签树的下行遍历3.2 标签树的上行遍历3.3 标签树的平行遍历4 bs4库的prettify()方法

1 BeautifulSoup库的解析器
代码语言:javascript
复制
soup = BeautifulSoup('<html>data</html>','html.parser')
2 BeautifulSoup类的基本元素
代码语言:javascript
复制
<p class="title"> ... </p>
3 基于bs4库的HTML内容遍历方法
3.1 标签树的下行遍历
代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.body.contents
代码语言:javascript
复制
['\n',
 <p class="title"><b>The demo python introduces several python courses.</b></p>,
 '\n',
 <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 class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>,
 '\n']
代码语言:javascript
复制
len(soup.body.contents)
代码语言:javascript
复制
5
代码语言:javascript
复制
# 遍历儿子节点
for child in soup.body.children: 
    print(child)
代码语言:javascript
复制
# 遍历子孙节点
for child in soup.body.descendants: 
    print(child)
3.2 标签树的上行遍历
代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.title.parent
代码语言:javascript
复制
<head><title>This is a python demo page</title></head>
代码语言:javascript
复制
for parent in soup.a.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)
代码语言:javascript
复制
p
body
html
[document]

遍历所有先辈节点,包括soup本身。

3.3 标签树的平行遍历

平行遍历发生在同一个父节点下的各个节点之间。

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.a.next_sibling
代码语言:javascript
复制
' and '
代码语言:javascript
复制
soup.a.previous_sibling
代码语言:javascript
复制
'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'
代码语言:javascript
复制
# 遍历后续节点
for sibling in soup.a.next_siblings:
    print(sibling)
代码语言:javascript
复制
# 遍历前续节点
for sibling in soup.a.previous_siblings:
    print(sibling)
4 bs4库的prettify()方法
代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.prettify()
代码语言:javascript
复制
'<html>\n <head>\n  <title>\n   This is a python demo page\n  </title>\n </head>\n <body>\n  <p class="title">\n   <b>\n    The demo python introduces several python courses.\n   </b>\n  </p>\n  <p class="course">\n   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">\n    Basic Python\n   </a>\n   and\n   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">\n    Advanced Python\n   </a>\n   .\n  </p>\n </body>\n</html>'

.prettify()方法为HTML文本<>及其内容增加'\n'。

参考资料:

[1] 中国大学MOOC: Python网络爬虫与信息提取(https://www.icourse163.org/course/BIT-1001870001

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据处理与编程实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 BeautifulSoup库的解析器
  • 2 BeautifulSoup类的基本元素
  • 3 基于bs4库的HTML内容遍历方法
    • 3.1 标签树的下行遍历
      • 3.2 标签树的上行遍历
        • 3.3 标签树的平行遍历
        • 4 bs4库的prettify()方法
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档