作家
登录

Python爬虫之BeautifulSoup

作者: 来源: 2017-06-14 17:04:45 阅读 我要评论

,id='value'

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

4. limit参数:find_all() 办法返回全部的搜刮构造,如不雅文档树很大年夜那么搜刮会很慢.如不雅我们不须要全部结不雅,可以应用 limit 参数限制返回结不雅的数量.效不雅与SQL中的limit关键字类似,当搜刮到的结不雅数量达到 limit 的限制时,就停止搜刮返回结不雅.

文档树中有3个tag相符搜刮前提,但结不雅只返回了2个,因为我们限制了返回数量,代码如下:

  1. soup.find_all("a", limit=2) 
  2.  
  3. # [<a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/elsie" id="link1">Elsie</a>, 
  4.  
  5. #  <a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/lacie" id="link2">Lacie</a>] 
  1. soup.find_all(text="Elsie"
  2.  
  3. # [u'Elsie'
  4.  
  5. soup.find_all(text=["Tillie""Elsie""Lacie"]) 
  6.  
  7. # [u'Elsie', u'Lacie', u'Tillie'
  8.  
  9. soup.find_all(text=re.compile("Dormouse")) 
  10.  
  11. [u"The Dormouse's story", u"The Dormouse's story"
  1. print soup.select('.sister'
  2.  
  3. #[<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")

经由过程标签名查找

  1. print soup.select('title'
  2.  
  3. #[<title>The Dormouse's story</title>] 
  4.   
  5.  
  6. print soup.select('a'
  7.  
  8. #[<a class="sister" href=http://developer.51cto.com/art/201706/"http://example.com/elsie" id="link1"><!

      推荐阅读

      华为发力云端 撬动城市云计算产业升级

    【51CTO.com原创稿件】进入2017年,记者异常明显地感到到,云办事的成长,已经成为城市之间比拼实力的重要赛道>>>详细阅读


    本文标题:Python爬虫之BeautifulSoup

    地址:http://www.17bianji.com/lsqh/35763.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)