网络爬虫:Beautiful Soup库&&信息组织与提取

2023-05-16

爬虫:Beautiful Soup库&&信息组织与提取

Copyright: Jingmin Wei, Pattern Recognition and Intelligent System, School of Artificial and Intelligence, Huazhong University of Science and Technology

网络爬虫专栏链接


文章目录

      • 爬虫:Beautiful Soup库&&信息组织与提取
        • Reference
        • 一、安装Beautiful Soup库
        • 二、BeautifulSoup库(HTML标签树)的基本元素
        • 三、基于bs4库的HTML内容遍历方法
        • 四、基于bs4库的HTML格式输出
        • 五、bs4库小结
        • 六、信息标记的三种形式
        • 七、三种信息标记形式的比较
        • 八、信息提取的一般方法
        • 九、基于bs4库的HTML内容查找方法
        • 十、信息标记和提取方法总结


本教程主要参考中国大学慕课的 Python 网络爬虫与信息提取,为个人学习笔记。

在学习过程中遇到了一些问题,都手动记录并且修改更正,保证所有的代码为有效。且结合其他的博客总结了一些常见问题的解决方式。

本教程不商用,仅为学习参考使用。如需转载,请联系本人。

Reference

爬虫 MOOC

数据分析 MOOC

廖雪峰老师的 Python 教程

一、安装Beautiful Soup库

不同于idle的输出,在输出内容后我手动加了一行空行,如果对于输入输出有疑问,可以自行打开python shell进行验证。

Win 平台: “以管理员身份运行” cmd

pip3 install beautifulsoup4

安装小测:

演示 HTML 页面地址:http://python123.io/ws/demo.html

文件名称:demo.html

在这里插入图片描述

页面源代码:HTML 5.0 格式代码

<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>

使用Requests库获取页面源代码

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> r.text
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\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:\r\n<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>\r\n</body></html>'

>>> demo = r.text

紧跟上述例子,使用 BeautifulSoup 解析 http://python123.io/ws/demo.html 的源代码

>>> from bs4 import BeautifulSoup
>>> 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:
   <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 格式总结

第一个参数为需要解析的 html 格式信息,第二个参数为解析"这锅汤"对应的解析器。

from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>data</p>', 'html.parser')

二、BeautifulSoup库(HTML标签树)的基本元素

Beautiful Soup 库是解析、遍历、维护“标签树”的功能库。

在这里插入图片描述

html 格式理解:

在这里插入图片描述

Beautiful Soup 库,也叫 beautifulsoup4 或 bs4。约定引用方式如下,即主要是用 BeautifulSoup 类。

from bs4 import BeautifulSoup
import bs4

实质上,BeautifulSoup对应一个HTML/XML文档的全部内容。

在这里插入图片描述

Beautiful Soup 库解析器:

解析器使用方法条件
bs4的HTML解析器BeautifulSoup(mk,‘html.parser’)安装bs4库
lxml的HTML解析器BeautifulSoup(mk,‘lxml’)pip install lxml
lxml的XML解析器BeautifulSoup(mk,‘xml’)pip install lxml
html5lib的解析器BeautifulSoup(mk,‘html5lib’)pip install html5lib

BeautifulSoup 类的基本元素

<p class=“title”></p>
基本元素说明
Tag标签,最基本的信息组织单元,分别用 <> 和 </> 标明开头和结尾
Name标签的名字,<p>…</p> 的名字是 ‘p’ ,格式:<tag>.name
Attributes标签的属性,字典形式组织,格式:<tag>.attrs
NavigableString标签内非属性字符串,<>…</> 中字符串,格式:<tag>.string
Comment标签内字符串的注释部分,一种特殊的 Comment 类型

回顾 html 代码( Markdown 自带 html 生成器):

This is a python demo page This is a python demo page

The demo python introduces several python courses.

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses: Basic Python and Advanced Python .

1.Tag 标签

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> demo = r.text

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.title
<title>This is a python demo page</title>

>>> tag = soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>

2.Tag 的 name(名字)

接上段代码

>>> soup.a.name
'a'

>>> soup.a.parent.name
'p'

>>> soup.a.parent.parent.name
'body'

3.Tag 的 attrs(属性)

接上段代码

>>> tag.attrs	#标签的属性。给出标签名字,属性名字以及属性的值之间的对应关系
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}

>>> tag.attrs['class']	#获取class属性的值
	      
['py1']

>>> tag.attrs['href']	#获取a标签的链接属性
	      
'http://www.icourse163.org/course/BIT-268001'

>>> type(tag.attrs)	#标签属性的类型
	      
<class 'dict'>

>>> type(tag)	#标签类型
	      
<class 'bs4.element.Tag'>

4.Tag 的 NavigableString(非属性字符串)

>>> soup.a
	      
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>

>>> soup.a.string
	      
'Basic Python'

>>> soup.p
	      
<p class="title"><b>The demo python introduces several python courses.</b></p>

>>> soup.p.string
	      
'The demo python introduces several python courses.'

>>> type(soup.p.string)
	      
<class 'bs4.element.NavigableString'>

5.Tag 的 Comment(注释)

html注释类型:<!--This is a comment>

>>> newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>", "html.parser")
	      
>>> newsoup.b.string
	      
'This is a comment'

>>> type(newsoup.b.string)
	      
<class 'bs4.element.Comment'>

>>> newsoup.p.string
	      
'This is not a comment'

>>> type(newsoup.p.string)
	      
<class 'bs4.element.NavigableString'>

两个字符串都会显示,但是我们应该根据类型判断到底是注释还是非属性字符串。

总结:

在这里插入图片描述

三、基于bs4库的HTML内容遍历方法

HTML 树形结构:

在这里插入图片描述

遍历方式:

在这里插入图片描述

HTML 源代码:

<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>

1.标签树的下行遍历

属性说明
.contents子节点的列表,将 <tag> 所有儿子节点存入列表
.children子节点的迭代类型,与 .contents 类似,用于循环遍历儿子节点
.descendants子孙节点的迭代类型,包含所有子孙节点,用于循环遍历

BeautifulSoup 类型是标签树的根节点

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo, "html.parser")
>>> soup.head
	      
<head><title>This is a python demo page</title></head>

>>> soup.head.contents	#子节点的列表
	      
[<title>This is a python demo page</title>]


>>> soup.body
	      
<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>

>>> soup.body.contents
	      
['\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']

>>> len(soup.body.contents)	#获取个数
	      
5

>>> soup.body.contents[1]	#通过索引检索列表信息
	      
<p class="title"><b>The demo python introduces several python courses.</b></p>

遍历孩子结点:

>>> for child in soup.body.children:
	      print(child)

	      


<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>


>>> for child in soup.body.descendants:
	      print(child)

	      


<p class="title"><b>The demo python introduces several python courses.</b></p>
<b>The demo python introduces several python courses.</b>
The demo python introduces several python courses.


<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>
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>
Basic Python
 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
Advanced Python

2.标签树的上行遍历

属性说明
.parent节点的父亲标签
.parents节点先辈标签的迭代类型,用于循环遍历先辈节点
>>> soup.title.parent
	      
<head><title>This is a python demo page</title></head>

>>> soup.html.parent	#HTML的标签的父亲即为其本身
	      
<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>

>>> soup.parent

接上段代码

>>> for parent in soup.a.parents:
	      if parent is None:
		      print(parent)
	      else:
		      print(parent.name)

	      
p
body
html
[document]

3.标签树的平行遍历

属性说明
.next_sibling返回按照 HTML 文本顺序的下一个平行节点标签
.previous_sibling返回按照 HTML 文本顺序的上一个平行节点标签
.next_siblings迭代类型,返回按照 HTML 文本顺序的后续所有平行节点标签
.previous_siblings迭代类型,返回按照 HTML 文本顺序的前续所有平行节点标签

在这里插入图片描述

>>> soup.a.next_sibling	#在标签树中树形结构采用标签的形式组织,但是标签树之间的非属性字符串也构成了标签树的结点,所以并不能想当然认为标签结点获得的下一个结点类型一定是标签类型。
	      
' and '
>>> soup.a.next_sibling.next_sibling
	      
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>

>>> soup.a.previous_sibling
	      
'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'

>>> soup.a.previous_sibling.previous_sibling	#空节点
	
>>> soup.a.parent	#父亲结点是一个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>
>>> for sibling in soup.a.next_sibling:
	      print(sibling)	#遍历后续结点

	      
 
a
n
d
 
>>> for sibling in soup.a.previous_sibling:
	      print(sibling)	#遍历前续结点
	#内容过长,请自行用idle或者pycharm、eclipse等输入验证

总结:

在这里插入图片描述

四、基于bs4库的HTML格式输出

能否让 HTML 内容更加“友好”的显示?

>>> import requests
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> demo = r.text
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo, "html.parser")
>>> soup.prettify()
	      
'<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>'
>>> 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:
   <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>

.prettify() 为 HTML 文本 <> 及其内容增加更加 ‘\n’ ,.prettify() 可用于标签,方法:<tag>.prettify()

>>> print(soup.a.prettify())
	      
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
 Basic Python
</a>

bs4库的编码

bs4 库将任何 HTML 输入都变成 utf‐8 编码,Python 3.x 默认支持编码是 utf‐8 ,解析无障碍。

>>> soup = BeautifulSoup("<p>中文</p>", "html.parser")
	      
>>> soup.p.string
	      
'中文'
>>> print(soup.p.prettify())
	      
<p>
 中文
</p>

五、bs4库小结

在这里插入图片描述

六、信息标记的三种形式

在这里插入图片描述

信息标记:

标记后的信息可形成信息组织结构,增加了信息维度

标记的结构与信息一样具有重要价值

标记后的信息可用于通信、存储或展示

标记后的信息更利于程序理解和运用

HTML 信息标记:

HTML - 超文本标记语言,能够将超文本的信息嵌入到文本之中

在这里插入图片描述

在这里插入图片描述

信息标记的三种形式:

1.XML(eXtensible Markup Language)

基于 HTML 格式发展而来的一种通用信息表达形式。

在这里插入图片描述
在这里插入图片描述

<img src=“china.jpg” size=“10”></img>
<img src=“china.jpg” size=“10” />
<!‐‐ This is a comment, very useful ‐‐>

在这里插入图片描述

2.Json(JavsScript Object Notation)

有类型的键值对构成的表达形式。给出信息,并给信息类型做定义。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

"name":"北京理工大学"
"name":["北京理工大学""延安自然科学院"]
"name":{
    "newName":"北京理工大学",
    "oldName":"延安自然科学院"
}

在这里插入图片描述
在这里插入图片描述

3.YAML(YAML Ain’t Markup Language)

无类型键值对 key:value
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

name:北京理工大学
name:
	newname:北京理工大学
	oldname:延安自然科学院
name:
-北京理工大学
-延安自然科学院
text:|	#学校介绍
北京理工大学创立于1940年,前身是延安自然科学院,是中国共产党创办的第一所理工科大学,毛泽东同志亲
自题写校名,李富春、徐特立、李强等老一辈无产阶级革命家先后担任学校主要领导。学校是新中国成立以来
国家历批次重点建设的高校,首批进入国家“211工程”和“985工程”建设行列;在全球具有广泛影响力的英
国QS“世界大学500强”中,位列入选的中国大陆高校第15位。学校现隶属于工业和信息化部。

在这里插入图片描述

七、三种信息标记形式的比较

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

XML:

<person>
	<firstName>Tian</firstName>
	<lastName>Song</lastName>
	<address>
		<streetAddr>中关村南大街5号</streetAddr>
		<city>北京市</city>
		<zipcode>100081</zipcode>
	</address>
	<prof>Computer System</prof><prof>Security</prof>
</person>

JSON:

{
	"firstName" : "Tian" ,
	"lastName" : "Song" ,
	"address" : {
		"streetAddr" : "中关村南大街5号" ,
		"city" : "北京市" ,
		"zipcode" : "100081"
				},
	"prof" : [ "Computer System" , "Security" ] 
}

YAML:

firstName : Tian
lastName : Song
address :
	streetAddr : 中关村南大街5号
	city : 北京市
	zipcode : 100081
prof : 
‐Computer System
‐Security

XML 最早的通用信息标记语言,可扩展性好,但繁琐

JSON 信息有类型,适合程序处理(js),较 XML 简洁

YAML 信息无类型,文本信息比例最高,可读性好

XML Internet上的信息交互与传递(包括特殊的 HTML 格式)

JSON 移动应用云端和节点的信息通信,无注释(程序对接口处理的情况)

YAML 各类系统的配置文件,有注释易读

八、信息提取的一般方法

从标记后的信息中提取所关注的内容


方法一:完整解析信息的标记形式,再提取关键信息

XML JSON YAML

需要标记解析器,例如:bs4 库的标签树遍历

优点:信息解析准确

缺点:提取过程繁琐,速度慢


方法二:无视标记形式,直接搜索关键信息

搜索

对信息的文本查找函数即可

优点:提取过程简洁,速度较快

缺点:提取结果准确性与信息内容相关


融合方法:结合形式解析与搜索方法,提取关键信息

XML JSON YAML 搜索

需要标记解析器及文本查找函数


实例:提取 HTML 中的所有 URL 链接

思路: 1) 搜索到所有<a>标签

​ 2) 解析<a>标签格式,提取href后的链接内容

>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get("https://python123.io/ws/demo.html")
>>> demo = r.text
>>> soup = BeautifulSoup(demo, "html.parser")
>>> for link in soup.find_all('a'):
		print(link.get('href'))

	
http://www.icourse163.org/course/BIT-268001
http://www.icourse163.org/course/BIT-1001870001

九、基于bs4库的HTML内容查找方法

<>.find_all(name, attrs, recursive, string, **kwargs)

返回一个列表类型,存储查找的结果

<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>

name : 对标签名称的检索字符串

>>> soup.find_all('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

>>> soup.find_all(['a', 'b'])
[<b>The demo python introduces several python courses.</b>, <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

>>> for tag in soup.find_all(True):
		print(tag.name)

	
html
head
title
body
p
b
p
a
a

>>> import re
>>> for tag in soup.find_all(re.compile('b')):
		print(tag.name)

	
body
b

attrs: 对标签属性值的检索字符串,可标注属性检索

>>> soup.find_all('p', 'course')	#是否包含'course'的信息
[<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>]

>>> soup.find_all(id='link1')	#全匹配搜索
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>]

>>> soup.find_all(id='link')	#全匹配搜索
[]

>>> import re
>>> soup.find_all(id=re.compile('link'))	#非全匹配搜索
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

recursive: 是否对子孙全部检索,默认 True

>>> soup.find_all('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>, <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]

>>> soup.find_all('a', recursive=False)	#说明a标签在子孙的结点层次中
[]

string: <>…</> 中字符串区域的检索字符串

>>> soup.find_all(string = "Basic Python")
['Basic Python']

>>> import re
>>> soup.find_all(string = re.compile("python"))
['This is a python demo page', 'The demo python introduces several python courses.']

<tag>() 等价于 <tag>.find_all()

soup() 等价于 soup.find_all()

方法扩展

方法说明
<>.find()搜索且只返回一个结果,同 .find_all() 参数
<>.find_parents()在先辈节点中搜索,返回列表类型,同 .find_all() 参数
<>.find_parent()在先辈节点中返回一个结果,同 .find() 参数
<>.find_next_siblings()在后续平行节点中搜索,返回列表类型,同 .find_all() 参数
<>.find_next_sibling()在后续平行节点中返回一个结果,同 .find() 参数
<>.find_previous_siblings()在前序平行节点中搜索,返回列表类型,同 .find_all() 参数
<>.find_previous_sibling()在前序平行节点中返回一个结果,同 .find() 参数

十、信息标记和提取方法总结

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

网络爬虫:Beautiful Soup库&&信息组织与提取 的相关文章

  • 12.单调栈——解决接雨水和柱状图中的最大矩形等问题

    单调栈 1 单调栈实现结构 单调栈解决的问题 xff1a 给你一个数组 想要用尽可能低的代价知道数组中每一个元素的左边元素比它大的或者右边元素比他大的信息是什么 如果用暴力方法 xff0c 左边遍历一次右边遍历一次 xff0c 时间复杂度为
  • 12.快速排序

    1荷兰国旗问题 问题1 xff1a 给定一个数组arr和一个数num xff0c 将小于等于num的数放在数组的左边大于num的数放在数组的右边 xff08 不要求有序 xff09 要求额外空间复杂度为O 1 时间复杂度为O N 遍历数组元
  • 死锁预防、死锁避免、死锁检测

    死锁 1 死锁的概念 1 1死锁的定义 多个进程并发执行 xff0c 由于竞争资源而造成的一种僵局 xff08 互相等待 xff09 xff0c 若无外力作用 xff0c 这些进程都将无法推进 xff0c 这就是死锁现象 例如 xff1a
  • 内存分配方式

    内存分配方式 1 基本概念 内存管理的基本概念 虽然计算机硬件发展 xff0c 内存容量在不断变大 xff0c 但是也不可能将所有用户进程和系统所需要的程序和数据放入内存中 xff0c 因此操作系统必须要对内存空间进行合理划分和有效动态分配
  • 虚拟内存和LRU页面置换算法

    虚拟内存 1 虚拟内存的基本概念 传统存储管理方式的特征 传统的内存管理策略都是为了同时将多个进程保存进内存中 xff0c 它们具有以下的共同特征 xff1a 一次性 作业必须一次性全部装入内存后 xff0c 才能开始运行 xff08 静态
  • 0.0C++和C的区别

    C 43 43 和C的区别 C 43 43 如今是一个同时支持面向过程 面向对象 函数形式 泛型形式 元编程形式的语言 我们该如何理解C 43 43 这门语言呢 xff1f Effective C 43 43 书中给出了一个简单的方法 xf
  • 15.9为什么要将成员变量设置为private

    为什么要将成员变量声明为private 为什么要将成员变量封装为private xff0c 主要有以下四个原因 xff1a 好处1 xff1a 如果成员变量不是public xff0c 那么客户唯一能访问成员变量的唯一方式就是通过成员函数
  • 2.7.C++中static关键字的5种基本用法

    static关键字 static关键字主要应用于以下几种情况 xff1a 情况1 xff1a static静态函数 定义静态函数 xff1a 在函数返回类型前加上static关键字 xff0c 函数即被定义为静态函数 静态函数只能在本源文件
  • 进程调度算法

    进程调度 在多道程序系统中 xff0c 进程数量往往多于处理机的个数 xff0c 因此进程竞争使用处理机的情况在所难免 处理机调度是对处理机进行分配 xff0c 即从就绪队列中按照一定的算法选择一个进程并将处理机分配给它运行 xff0c 以
  • git clone 出现fatal: unable to access ‘https://github 类错误解决方法

    git clone 遇到问题 xff1a fatal unable to access https github comxxxxxxxxxxx Failed to connect to xxxxxxxxxxxxx 问题 将命令行里的http
  • 进程通信的方式

    进程通信 1 进程通信的概念 进程是一个独立的资源分配单元 xff0c 不同进程 xff08 主要是指不同的用户进程 xff09 之间的资源是独立的 xff0c 没有关联的 xff0c 不能在一个进程中直接访问另一个进程的资源 但是 xff
  • 网络通信的过程

    网络通信的过程 封装 上层协议时如何使用下层协议提供的服务的呢 xff1f 其实这是通过封装实现的 应用程序是在发送到物理网络上之前 xff0c 将沿着协议栈从上往下依次传递 每层协议都将在上层数据的基础上加上自己的头部信息 xff08 有
  • TCP三次握手、四次挥手

    TCP通信流程 TCP和UDP TCP和UDP区别如下 xff1a UDP xff1a 用户数据报文协议 xff0c 面向无连接 xff0c 可以单播 xff0c 多播 xff0c 广播 xff0c 面向数据报 xff0c 不可靠 TCP
  • Qt的多线程编程

    Qt线程 基本概念 并发 当有多个线程在操作时 xff0c 如果系统只有一个CPU xff0c 则它根本不可能真正同时进行一个以上的线程 xff0c 它只能把CPU运行时间划分成若干个时间段 xff0c 再将时间段分配给各个线程执行 xff
  • CMake编译C++文件

    这篇文章介绍如何使用cmake工具编译一个最简单的helloworld cpp文件 首先创建一个空的文件夹 mkdir cmake test 在该文件夹下 xff0c 我们新建一个helloworld cpp文件 span class to
  • 智能小车建图导航-在rviz中导航(运行)

    笔记来源 xff1a 机器人开发与实践 xff08 古月 xff09 或者直接运行这个脚本文件 xff1a xff08 如果你没有在 bracsh文件中加入source xff0c 建议加入或者在脚本文件的上面中添加source xff0c
  • 004-S500无人机-相关的器件参数以及计算

    这篇博客主要是记录S500无人机的相关器件的参数 xff0c 参数的来源来源于holybro官网 xff1a https shop holybro com 我这里进行参数的归纳以及计算 一 电机 xff08 2216 880kv xff09
  • TX2 学习记录(开启板载/USB摄像头)

    刚拿到手一个TX2 xff0c 简单地学习一下这块板子 xff0c 因为是学长留下来的板子 xff0c 所以刷机的步骤我就省略了 xff0c 各位小伙伴可以参考其他大佬的博客进行刷机 xff0c 再来就记录一下一些操作指令吧 打开USB摄像
  • ubuntu16.04中进行ROS通信编程

    ROS通信学习 基础知识学习字段ROS通信小例子一 创建一个工作区二 创建一个ROS工程包三 创建通信的发 收节点四 测试程序的正确性 图像ROS通信小例子视频ROS通信小例子多机ROS通信 基础知识学习 x1f31f 话题与服务的区别 话
  • 2021电赛F题智能送药小车方案分析(openMV数字识别,红线循迹,STM32HAL库freeRTOS,串级PID快速学习,小车自动返回)

    2021全国大学生电子设计竞赛F题智能送药小车 前提 xff1a 本篇文章重在分享自己的心得与感悟 xff0c 我们把最重要的部分 xff0c 摄像头循迹 xff0c 摄像头数字识别问题都解决了 xff0c 有两种方案一种是openARTm

随机推荐

  • CARLA常见错误解决方案以及常见的问题解决方案

    记录Linux环境 Windows环境下常见的运行自动驾驶仿真器CARLA出现的错误 问题1 问题1比较基础 xff0c 创建虚拟环境以及删除虚拟环境 conda create span class token operator span
  • cmd找不到conda以及通过cmd启用Anaconda中的Python环境(base)

    问题 xff1a 在cmd中输入python无法进入或启用python ipython conda jupyter notebook 一 解决方法 xff1a 在系统环境中添加Anaconda路径 lt 1 gt 1 打开高级系统设置 xf
  • c语言实现strcat函数

    char strcat char strDestination const char strSource 一 函数介绍 作用 xff1a 连接字符串的函数 xff0c 函数返回指针 xff0c 两个参数都是指针 xff0c 第一个参数所指向
  • C/C++的static关键字作用(转载)

    一 限制符号的作用域只在本程序文件 若变量或函数 xff08 统称符号 xff09 使用static修饰 xff0c 则只能在本程序文件内使用 xff0c 其他程序文件不能调用 xff08 非static的可以通过extern 关键字声明该
  • crc校验

    参考链接 xff1a https www cnblogs com esestt archive 2007 08 09 848856 html 一 CRC校验原理 1 CRC校验全称为循环冗余校验 xff08 Cyclic Redundanc
  • ubuntu安装eclipse教程

    在安装eclipse之前 xff0c 要先安装JDK xff0c 一 安装JDK 1 从官网上下载JDK 链接 xff1a https www oracle com java technologies downloads 选择的jdk文件一
  • UDP通信入门篇

    UDP通信属于网络通信中的一种方式 xff0c 需要用套接字来进行通信 初接触UDP通信时 xff0c 不知道需要链接静态库 pragma comment lib ws2 32 lib xff0c 导致自己在前期浪费了很多时间去排查问题 除
  • window11配置深度学习环境

    Anaconda 43 PyCharm 43 CUDA 43 CUDNN 43 PyTorch 1 Anaconda安装 下载路径 xff1a https www anaconda com 安装方式 xff1a 以管理员身份安装 中间选项
  • python配置opencv环境后,读取图片,报错:can‘t open/read file: check file path/integrity

    运行出错代码 xff1a import cv2 import numpy as np image 61 cv2 imread 39 C Pictures 桌面背景图片切换 wallhaven 6oq1k7 jpg 39 cv2 IMREAD
  • 断言

    代码中放置一些假设 xff0c 通过判断假设是否为真 xff0c 进而判断程序是否正确 断言就是用来测试程序中的假设是否正确的 xff0c 若果假设被违反 xff0c 那么就中断程序的执行 断言assert是定义在assert h中的 宏
  • STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波

    SPWM波 对于功率方向 输出SPWM波是必须要掌握的 工程 stm32生成spwm代码Keil工程链接资源 引用spwm波定义 PWM波形就是指占空比可变的波形 SPWM波形是指脉冲宽度按正弦规律变化且和正弦波等效的PWM波形 两者的区别
  • C语言链表写法,练习链表

    C语言链表写法 xff0c 练习链表 建立要一个文件 xff1a LinkList h 内容 xff1a span class token macro property span class token directive keyword
  • 树莓派摄像头 C++ OpenCV YoloV3 实现实时目标检测

    树莓派摄像头 C 43 43 OpenCV YoloV3 实现实时目标检测 本文将实现树莓派摄像头 C 43 43 OpenCV YoloV3 实现实时目标检测 xff0c 我们会先实现树莓派对视频文件的逐帧检测来验证算法流程 xff0c
  • RTK定位原理

    一 卫星测距原理说明 天上的卫星发送数据被便携式RTK终端接收到 xff0c 卫星和终端之间的距离D 61 C T C为光速 xff0c T为卫星发送的信号到达便携式RTK终端的时间 xff0c 通过时间乘以距离可以获得卫星和便携式终端的实
  • 网络协议与网络编程(单电脑信息传输)

    C C 43 43 网络编程 单电脑信息传输 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Artificial and Intelli
  • Pytorch:全连接神经网络-MLP回归

    Pytorch 全连接神经网络 解决 Boston 房价回归问题 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Artificial a
  • Pytorch:卷积神经网络-空洞卷积

    Pytorch 空洞卷积神经网络 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Artificial and Intelligence
  • Pytorch:目标检测网络-人体关键点检测

    Pytorch 目标检测网络 人体关键点检测 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Artificial and Intelli
  • Pytorch:图像风格快速迁移

    Pytorch 图像风格快速迁移 残差网络 xff0c 固定风格任意内容 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Artifici
  • 网络爬虫:Beautiful Soup库&&信息组织与提取

    爬虫 xff1a Beautiful Soup库 amp amp 信息组织与提取 Copyright Jingmin Wei Pattern Recognition and Intelligent System School of Arti