zoukankan      html  css  js  c++  java
  • 爬虫库之BeautifulSoup学习(四)

    探索文档树:

    find_all(name,attrs,recursive,text,**kwargs)

    方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件

    1、name参数,可以查找所有名字为name的tag,字符串对象会被自动忽略掉。

    1) 传字符串

    最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容.

    下面的例子用于查找文档中所有的<b>标签

    soup.find_all('b')

    # [<b>The Dormouse's story</b>]

     

    2)传正则表达式

    如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.

    下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到

    import re

    for tag in soup.find_all(re.compile("^b")):

      print tag.name

    #body

    #b

     

    3)传列表

    如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签

    soup.find_all(["a","b"])

    # [<b>The Dormouse's story</b>,
    # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
    # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
    # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

     

    4)传True

    可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点

    5)传方法

    2.keyword参数

      注意:如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 id 的参数,Beautiful Soup会搜索每个tag的”id”属性

      soup.find_all(id='link2')

      # [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

       

      如果传入href参数,BeautifulSoup会搜索每个tag的"href"属性

      soup.find_all(href=re.compile("elsie"))

      使用多个指定名字的参数可以同时过滤tag的多个属性

      soup.find_all(href=re.compile("elsie"),id='link1)

      

      在这里我们想用 class 过滤,不过 class 是 python 的关键词,这怎么办?加个下划线就可以

      soup.find_all("a",class_="sister")  

    # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
    # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
    # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

    3、text参数

      通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True  

    soup.find_all(text="Elsie")
    # [u'Elsie']

    soup.find_all(text=["Tillie", "Elsie", "Lacie"])
    # [u'Elsie', u'Lacie', u'Tillie']

    soup.find_all(text=re.compile("Dormouse"))
    [u"The Dormouse's story", u"The Dormouse's story"]

     

    4、limit参数

      可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.

      soup.find_all('a',limit=2) 

    [<a class="mnav" href="http://news.baidu.com" name="tj_trnews">u65b0u95fb</a>,

    <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>]
    [Finished in 0.3s]

      

     

     

  • 相关阅读:
    每日一篇文献:Robotic pick-and-place of novel objects in clutter with multi-affordance grasping and cross-domain image matching
    每日一篇文献:Intuitive Bare-Hand Teleoperation of a Robotic Manipulator Using Virtual Reality and Leap Motion
    每日一篇文献:Virtual Kinesthetic Teaching for Bimanual Telemanipulation
    HEBI Robotic Arm VR Teleoperation
    「iQuotient Case」AR device teleoperated robotic arm
    VR and Digital Twin Based Teleoperation of Robotic Arm
    HEBI Robotic Arm VR Teleoperation
    Human Robot Interaction
    Immersive Teleoperation Project
    机器人演示学习
  • 原文地址:https://www.cnblogs.com/yu2000/p/6852501.html
Copyright © 2011-2022 走看看