python网络爬虫与信息提取-03-beautifulsoup库

bs4库的基本元素

Tag Name Attributes NavigableString Comment

bs4库的遍历功能

.contents
.parent
.next_sibling
.children
.parents
.previous_sibling
.descendants
.next_siblings
.previous_siblings

信息的标记

  • 标记后的信息可形成信息组织结构,增加了信息维度
  • 标记后的信息可用于通信、存储或展示
  • 标记的结构与信息一样具有重要价值
  • 标记后的信息更利于程序理解和运用

HTML的信息标记

python网络爬虫与信息提取-03-beautifulsoup库
python网络爬虫与信息提取-03-beautifulsoup库

信息标记的三种形式

python网络爬虫与信息提取-03-beautifulsoup库
python网络爬虫与信息提取-03-beautifulsoup库

信息提取的一般方法

python网络爬虫与信息提取-03-beautifulsoup库
python网络爬虫与信息提取-03-beautifulsoup库

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

思路:1.搜索到所有标签;2.解析标签格式,提取href后的链接内容。

<>.find_all(name,attrs,recursive,string,**kwargs)
返回一个列表类型,存储查找的结果。
name:对标签名称的检索字符串。
attrs:对标签属性值的检索字符串,可标注属性检索。
recursive:是否对子孙全部检索,默认True。
string:<>…</>中文字符串区域的检索字符串。