[关闭]
@nemos 2017-05-06T02:01:10.000000Z 字数 2519 阅读 798

beautifulsoup

py


解析器

基本用法

  1. from bs4 import BeautifulSoup
  2. soupobj = BeautifulSoup('htmltext', 'html.parser') #将文本转换BS对象,指定解析器
  3. betterhtml = soupobj.prettify() #转换为xhtml,优化格式

四大对象种类

示例HTML

  1. '''
  2. <html>
  3. <head><title>The Dormouse's story</title></head>
  4. <body>
  5. <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
  6. <p class="story">Once upon a time there were three little sisters; and their names were
  7. <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
  8. ,
  9. <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
  10. and
  11. <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
  12. ;and they lived at the bottom of a well.</p>
  13. <p class="story">...</p>
  14. </body>
  15. </html>
  16. '''

Tag

HTML标签加上标签内的内容即为Tag

  1. soup.tag #可直接获得tag
  2. #第一个符合的Tag
  1. >>> soup.title
  2. title>The Dormouse's story</title>
  3. >>> soup.p
  4. <p class="title" name="dromouse"><b>The Dormouse's story</b></p>

Tag属性

  1. name属性
  1. >>> soup.head.name #标签的名字即为自身
  2. head
  1. attrs属性
  1. >>> soup.p.attrs #HTML元素属性的字典
  2. {'class': ['title'], 'name': 'dromouse'}
  3. >>> soup.p['class']
  4. >>> soup.p.attrs['class']
  5. >>> soup.p.get('class') #以上三种全部等价
  6. ['title']
  1. >>> soup.p.string #获得标签内部的文字
  2. The Dormouse's story

BeautifulSoup

  1. >>> soup.name #BeautifulSoup即是一个特殊的Tag对象
  2. [document]

Comment

特殊的NavigableString
即注释内容
类型为bs4.element.Comment

遍历文档树

直接子节点

  1. soup.tag.contents #以list形式返回tag的子节点
  2. soup.tag.children #返回list的生成器对象
  3. soup.tag.descendants#返回所有子孙节点

节点内容

  1. soup.tag.string #返回节点内容
  2. #可返回唯一子节点的内容,多个内容则为None
  3. soup.tag.strings #获取多个内容
  4. soup.tag.stripped_strings#去除了空白符的字符串

父节点

  1. soup.tag.parent
  2. soup.head.title.string.parent.name #还是title
  3. soup.tag.parents

兄弟节点

  1. soup.tag.next_sibling #前兄弟节点
  2. soup.tag.prev_sibling
  3. soup.tag.next_siblings
  4. soup.tag.prev_siblings

前后节点

  1. soup.tag.next_element #获得前后节点
  2. soup.tag.previous_element#前后节点不分层次

搜索文档树

  1. #搜索当前tag的所有tag子节点,并判断是否符合过滤器条件
  2. find_all(name,#传字符串查找相应的tag
  3. #传正则会调用match来匹配tag
  4. #传列表将任意一匹配元素返回
  5. #传True匹配任何值
  6. #传方法以tag为参数,返回True则匹配
  7. #
  8. attrs, #可以知指定特殊的属性如attrs={'data-foo' : 'value'}
  9. recursive,#如果只想搜索直接子节点点,可指定为False
  10. text, #搜索文档中的字符串内容
  11. limit, #限制返回个数
  12. **kwargs) #可过滤指定的tag属性如id='id1',href=re.compile('some')
  13. #关键字加下划线,class_='class1'
  1. find() #类似find_all,但只返回一个节点
  2. find_parents() #找父节点
  3. find_parent()
  4. find_next_siblings() #找下一个兄弟节点
  5. find_next_sibling()
  6. find_previous_siblings()#找前一个兄弟节点
  7. find_previos_sibling()
  8. find_all_next() #找下一个节点,无层次关系
  9. find_next()
  10. find_all_previous() #同上
  11. find_previos()

CSS选择器

  1. #返回列表,用get_text()方法获得内容
  2. soup.select('title') #标签名查找
  3. soup.select('.class1') #类名查找
  4. soup.select('#id1') #id查找
  5. soup.select('title #id1') #组合查找
  6. soup.select('head > title') #子标签查找
  7. soup.select('a[href="http://python.org"]')#属性查找
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注