美文网首页
爬虫之BeautifulSoup(二)--遍历标签树

爬虫之BeautifulSoup(二)--遍历标签树

作者: 五秋木 | 来源:发表于2017-11-10 14:52 被阅读0次

遍历标签树

  1. 下行遍历
.contents讲解
soup.head.contents   # 将所有的儿子节点存到列表中
len(soup.head.contents)  # 儿子节点数量
soup.head.contents[1]   # 获取第二个儿子节点信息

.children迭代
for child in soup.body.children
    print(child)   循环遍历 
.descendants  子孙节点,也可遍历
  1. 上行遍历
.parent  节点的父亲节点
标签树的上行遍历
soup = BeautifulSoup(demo,"html.parser")
for parent in soup.a.parents:
          if parent is none:(soup的parent无parent节点)
                   print(parent)
          else:
                  print(parent.name)
  1. 平行遍历
.next_sibling    返回下一个平行点标签
.previous_sibling   返回上一个平行点标签
.next_siblings     后续所有的平行点标签,迭代类型
.previous_siblings   前序所有的平行点标签,迭代类型

HTML格式化和编码

soup.prettify()

相关文章

  • 爬虫之BeautifulSoup(二)--遍历标签树

    遍历标签树 下行遍历 上行遍历 平行遍历 HTML格式化和编码 soup.prettify()

  • BeautifulSoup的使用姿势

    BeautifulSoup 是什么 BeautifulSoup库是解析、遍历、维护“标签树”的功能库 安装 注意:...

  • Python的BeautifulSoup包

    pip install BeautifulSoup4 Beautiful Soup库是解析、遍历、维护“标签树”的...

  • beautifulsoup 库(一)

    beautifulsoup(简称bs) 是解析、遍历、维护“标签树”的功能库。 标签概要 只要提供的是标签类型,b...

  • Python之Beautiful Soup库

    Beautiful Soup库是解析、遍历、维护“标签树”的功能库 BeautifulSoup对应一个HTML/X...

  • BeautifulSoup库-标签解析遍历

    本文主要内容:BeautifulSoup库的介绍,基本元素,标签树的遍历 安装:pip install Beaut...

  • 爬虫笔记2-解析

    BeautifulSoup 用于解析已经抓取的文件内容。是解析、遍历、维护“标签树”的功能库。 pip insta...

  • 爬虫2

    爬虫之 beautifulsoup BeautifulSoup3目前已经停止开发,推荐现在的项目使用Beautif...

  • 爬虫

    爬虫之 beautifulsoup BeautifulSoup3目前已经停止开发,推荐现在的项目使用Beautif...

  • 2.2.3 导航树

    掌握BeautifulSoup标签树的导航 子标签和后代标签:children()、descendants() 兄...

网友评论

      本文标题:爬虫之BeautifulSoup(二)--遍历标签树

      本文链接:https://www.haomeiwen.com/subject/nsrbmxtx.html