python beautiful soup库入门安装教程

 更新时间:2021年08月30日 14:31:21   作者:Cachel wood  
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。今天通过本文给大家分享python beautiful soup库入门教程,需要的朋友参考下吧

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 = requests.get("http://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="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>

Tag标签

基本元素 说明
Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
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="http://www.icourse163.org/course/BIT-268001" >Basic Python</a>

任何存在于HTML语法中的标签都可以用soup.访问获得。当HTML文档中存在多个相同对应内容时,soup.返回第一个

Tag的name

基本元素 说明
Name 标签的名字,

的名字是'p',格式:.name

import requests
from bs4 import BeautifulSoup
r = requests.get("http://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 = requests.get("http://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': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
http://www.icourse163.org/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 = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = BeautifulSoup(demo,"html.parser")
print(soup.head)
print(soup.head.contents)
print(soup.body.contents)
print(len(soup.body.contents))
print(soup.body.contents[1])
<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="http://www.icourse163.org/course/BIT-268001" >Basic Python</a> and <a  href="http://www.icourse163.org/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 = requests.get("http://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="http://www.icourse163.org/course/BIT-268001" >Basic Python</a> and <a  href="http://www.icourse163.org/course/BIT-1001870001" >Advanced Python</a>.</p>
</body></html>
import requests
from bs4 import BeautifulSoup
r = requests.get("http://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 = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = BeautifulSoup(demo,"html.parser")
print(soup.a.next_sibling)
print(soup.a.next_sibling.next_sibling)

print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)

print(soup.a.parent)
and 
<a class="py2" href="http://www.icourse163.org/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 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>
for sibling in soup.a.next_sibling:
	print(sibling)  #遍历后续节点
for sibling in soup.a.previous_sibling:
	print(sibling)  #遍历前续节点

在这里插入图片描述

bs库的prettify()方法

import requests
from bs4 import BeautifulSoup
r = requests.get("http://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="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </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库入门内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于python 爬虫爬到含空格的url的处理方法

    基于python 爬虫爬到含空格的url的处理方法

    今天小编就为大家分享一篇基于python 爬虫爬到含空格的url的处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • 关于pandas的read_csv方法使用解读

    关于pandas的read_csv方法使用解读

    这篇文章主要介绍了关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • 进一步探究Python中的正则表达式

    进一步探究Python中的正则表达式

    这篇文章主要介绍了Python中的正则表达式的一些用法,正则表达式的使用是Python学习进阶中的重要知识,需要的朋友可以参考下
    2015-04-04
  • 如何实现python爬虫爬取视频时实现实时进度条显示

    如何实现python爬虫爬取视频时实现实时进度条显示

    这篇文章主要介绍了如何实现python爬虫爬取视频时实现实时进度条显示,在爬取并下载网页上的视频的时候,我们需要实时进度条,这可以帮助我们更直观的看到视频的下载进度。文章围绕主题展开更多内容,需要的小伙伴可以参考一下
    2022-06-06
  • python库umap有效地揭示高维数据的结构和模式初探

    python库umap有效地揭示高维数据的结构和模式初探

    这篇文章主要介绍了python库umap有效地揭示高维数据的结构和模式初探,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Python中bisect的用法

    Python中bisect的用法

    这篇文章主要介绍了Python中bisect的用法,主要讲述了针对数组的插入及排序操作,非常具有实用价值,需要的朋友可以参考下
    2014-09-09
  • Python爬虫使用实例wallpaper问题记录

    Python爬虫使用实例wallpaper问题记录

    本文介绍解决中文乱码的方法,以及Python爬虫处理数据、图片URL的技巧,包括使用正则表达式处理字符串、URL替换等,还涉及单线程与多线程的应用场景,如电脑壁纸和手机壁纸爬取,适合进行Web数据抓取和处理的开发者参考
    2024-09-09
  • 对python3中, print横向输出的方法详解

    对python3中, print横向输出的方法详解

    今天小编就为大家分享一篇对python3中, print横向输出的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • python pandas处理excel表格数据的常用方法总结

    python pandas处理excel表格数据的常用方法总结

    在计算机编程中,pandas是Python编程语言的用于数据操纵和分析的软件库,下面这篇文章主要给大家介绍了关于python pandas处理excel表格数据的常用方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Python+SimpleRNN实现股票预测详解

    Python+SimpleRNN实现股票预测详解

    这篇文章主要为大家详细介绍了如何利用Python和SimpleRNN实现股票预测效果,文中的示例代码讲解详细,对我们学习有一定帮助,需要的可以参考一下
    2022-05-05

最新评论