3. text参数:经由过程 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接收 字符串 , 正则表达式 , 列表, True
4. limit参数:find_all() 办法返回全部的搜刮构造,如不雅文档树很大年夜那么搜刮会很慢.如不雅我们不须要全部结不雅,可以应用 limit 参数限制返回结不雅的数量.效不雅与SQL中的limit关键字类似,当搜刮到的结不雅数量达到 limit 的限制时,就停止搜刮返回结不雅.
文档树中有3个tag相符搜刮前提,但结不雅只返回了2个,因为我们限制了返回数量,代码如下:
- soup.find_all("a", limit=2)
- # [<a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/elsie" id="link1">Elsie</a>,
- # <a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/lacie" id="link2">Lacie</a>]
- 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"]
- print soup.select('.sister')
- #[<a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/lacie" id="link2">Lacie, Tillie]
5. recursive 参数:调用tag的 find_all() 办法时,BeautifulSoup会检索当前tag的所有子孙节点,如不雅只想搜刮tag的直接子节点,可以应用参数 recursive=False
find( name , attrs , recursive , text , **kwargs )
它与 find_all() 办法独一的差别是 find_all() 办法的返回结不雅是值包含一个元素的列表,而 find() 办法直接返回结不雅,就是直接返回第一匹配到的元素,不是列表,不消遍历,如soup.find("p").get("class")
经由过程标签名查找
- print soup.select('title')
- #[<title>The Dormouse's story</title>]
- print soup.select('a')
- #[<a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/elsie" id="link1"><!
推荐阅读
【51CTO.com原创稿件】进入2017年,记者异常明显地感到到,云办事的成长,已经成为城市之间比拼实力的重要赛道>>>详细阅读
地址:http://www.17bianji.com/lsqh/35763.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示