BeautifulSoup_find_ 用法和实例
2017-09-25 15:35
423 查看
Usage_BeautifulSoup_find_* 函数的使用
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2017-09-24 17:27:33 # @Author : kangvcar (kangvcar@126.com) # @Link : http://www.github.com/kangvcar/ # @Version : $Id$ from bs4 import BeautifulSoup import re html = """ The Dormouse's story The Dormouse's story Once upon a time there were three little sisters; and their names were , Lacie and Tillie; and they lived at the bottom of a well. ... """ soup = BeautifulSoup(html, "lxml") ######################## ###find_all()返回list#### ######################## ## 查找文档中所有的[b]标签,返回list # print soup.find_all('b') ## 查找文档中所有的标签,返回list # for i in soup.find_all('a'): # print i ## 找出所有以b开头的标签,返回list # print soup.find_all(re.compile("^b")) # for tag in soup.find_all(re.compile("^b")): # print tag ## 找到文档中所有标签和[b]标签,返回list # print soup.find_all(['a','b']) ## 找到所有的tag,但是不会返回字符串节点,返回list # for tag in soup.find_all(True): # print tag.name ## 传入方法,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False # def has_class_but_no_id(tag): # return tag.has_attr('class') and not tag.has_attr('id') # print soup.find_all(has_class_but_no_id) ## 匹配id=link2的tag # print soup.find_all(id='link2') ## Beautiful Soup会搜索每个tag的”href”属性 # print soup.find_all(href=re.compile("elsie")) ## 使用多个指定名字的参数可以同时过滤tag的多个属性 # print soup.find_all(href=re.compile("elsie"), id='link1') ## class 是 python 的关键词,这怎么办?加个下划线就可以 # print soup.find_all("a", class_="sister") ## 通过 text 参数可以搜搜文档中的字符串内容 ## 与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True # print soup.find_all(text="Elsie") # print soup.find_all(text=["Tillie", "Elsie", "Lacie"]) # print soup.find_all(text=re.compile("Dormouse")) ## 当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果. # print soup.find_all("a", limit=2) ## 调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False . # print soup.html.find_all("title") # print soup.html.find_all("title", recursive=False) ## find_all() 方法的返回结果是值包含一个元素的list ## find() 方法直接返回结果,非list # print soup.find('a') ## find_parents() find_parent() 用来搜索当前节点的父辈节点 # print soup.head.title.find_parents() # print soup.head.title.find_parent() ## find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,返回list ## find_next_sibling() 只返回符合条件的后面的第一个tag节点,非list # print soup.body.p.find_next_siblings() # print soup.body.p.find_next_sibling() ## find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点,返回list ## find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点,非list # print soup.body.find_previous_siblings() # print soup.body.find_previous_sibling() ## find_all_next() 方法返回所有符合条件的节点,返回list ## find_next() 方法返回第一个符合条件的节点,非list # print soup.head.find_all_next() # print soup.head.find_next() ## find_all_previous() 方法返回所有符合条件的节点,返回list ## find_previous() 方法返回第一个符合条件的节点,非list # print soup.head.title.find_all_previous() # print soup.head.title.find_previous()
相关文章推荐
- find用法及其实例
- beautifulSoup基本用法及find选择器
- Linux中find常见用法及实例
- Linux find命令实例教程 15个find命令用法
- Jquery中find与each方法用法实例
- Linux下which、whereis、locate、find区别与用法实例
- jQuery中find()方法用法实例
- Linux下find 命令用法详解+实例
- BeautifulSoup 用法和实例
- BeautifulSoup_CSS_Select 用法和实例
- Linux中find命令及常见用法实例
- Linux下find 命令用法详解+实例
- (转)Linux下find 命令用法详解+实例
- Linux下find 命令用法详解+实例
- Jquery中find与each方法用法实例
- Linux命令find的55种用法实例
- Jquery中find与each方法用法实例
- jQuery中find()方法用法实例
- Java接口和抽象类用法实例总结
- python time模块用法实例详解