python BeautifulSoup的简单用法

总结一下大概三种查找方法,1:直接查找,2:find_all和find,3:通过css选择器select()方法,



# _*_ coding:utf-8 _*_

from bs4 import BeautifulSoup
import re
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""


soup = BeautifulSoup(html,"html.parser")#""中指的是解析器,是bs4中自带的


#repr()函数,返回的值可以给解释器调用,具体用法后续再研究,跟str()好像差不多.str()对用户更友好一些?不能再次给解释器调用
#print soup.a
# print type(soup)
#print soup.a.string
#子节点.contents .children 属性
#tag 的.contents属性可以将tag的子节点以列表的形式输出
#print soup.head.contents#输出列表[<title>The Dormouse's story</title>]
#print soup.head.contents[0]#拿出列表的第一个元素
#children = soup.a.children #输出一个list生成器对象<listiterator object at 0x00000000020DDD30>
#   print child
#.descendants属性,子孙节点
#for child in soup.descendants:#跟children类似,需要用for in 打印出
#   print child
#print type(soup.a.children)
#如果一个tag里有多个标签,name .string就会返回none,如果一个标签里只有一个标签,那么string返回最里面内容
#for string in soup.strings:#.strings的用法,需要遍历。
#   print repr(string)
#for string in soup.stripped_strings:#stripped_strings的方法去除空格  可能会用的比较多
#   print string#不是用print repr(string),只使用print string也可以输出
#5::父节点.parent属性
#p = soup.p 
#print p.parent.name#结果是body
#print soup.p.parent.name#这种写法也可以输出........干嘛教程用上面的方法教???
#print soup.head.title.string.parent.name#这种写法太长了。。。。还是教程的方法好?
#6:parents的用法,可以递归得到元素左右父节点
#content = soup.head.title.string#注意要用元素string
#for parent in content.parents:#要用遍历的方法
#    print parent.name
#7:兄弟节点next_sibling,previous_sibling属性
#可以理解为获取该节点的同一级节点.
#print soup.p.next_sibling#此处为空白,next_sibling获取该节点的下一个兄弟节点,下一个是空白会返回空白
#print soup.p.prev_sibling#此处为none,previous_sibling获取该节点的前一个节点,如果之前没节点返回none
#8:全部兄弟节点next_siblings,previous_siblings。
#对当前节点的兄弟节点迭代输出
#print type(soup.p.next_siblings)#属性是generator????发生器。待理解
#for sibling in soup.p.next_siblings:#也是用遍历的方式。
#    print repr(sibling)#
#前后节点next_element previous_element.并不针对兄弟节点,而是前后节点都可以输出,不分层级
#print soup.head.next_element#之前的节点title并不是一个层级依然输出.
#print soup.head.previous_element#注意,这里是用previous_element,不像之前用previous_sibling.而且输出了所有内容?????
#所有前后节点,next_elements,previous_elements属性
#通过next_elements和previous_elements的迭代器就可以向前或向后输出解析内容,注意输出的是内容
#for element in soup.head.next_elements:#s输出之后的内容
#    print repr(element)
#for element in soup.head.previous_elements:
#    print repr(element)
#for element in last_a_tag.next_elements:#last_a_tag一直报警,好像是少引入什么东西了
#   print element
    


#find_all(name,attrs,recursive,text,**kwargs)方法
#搜索当前tag的所有tag子节点,并判断是否符合过滤器条件
#name参数,可以查找所有名字为name的tag,字符串对象会被忽略
#传字符串
#print soup.find_all('b')#[<b>The Dormouse's story</b>],得到所有<b>的标签和内容
#传正则表达式
#for tag in soup.find_all(re.compile("^b")):#传入正则表达式
#    print tag.name
#传列表
#print soup.find_all(["a","b"])#任意一个与列表 参数匹配的都会返回
#传true
#for tag in soup.find_all(True):#传不传True的结果一样
#    print tag.name
#传方法
#def  has_class_but_no_id(tag):
#    return tag.has_attr('class') and not tag.has_attr('id')#has_attr()是判断对象是否含这个属性,返回值true和false
#print soup.find_all(has_class_but_no_id)
#keyword参数 搜索tag中特定属性使用,
#print soup.find_all(id='link2')#结果:[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
#print soup.find_all(href=re.compile("elsie"))#传入正则
#print soup.find_all(href=re.compile("elsie"),id='link1')#传入两个属性活着多个属性
#print soup.find_all('a',class_="sister")#class是python关键词,在class后面加下划线可以用
#data不能用,但是可以定义字典来使用,,例如print soup.find_all(attrs={"data-foo":"value"})
#text参数
#通过text参数可以搜索文档中字符串内容,与name参数一样可以传入字符串,正则,参数,True
#print soup.find_all(text="Elsie")#Elsie在注释中,是打印不出来的。。。。。例子有错
#print soup.find_all(text=["Tillie","Elise","Lacie"])
#print soup.find_all(text=re.compile("Dormouse"))#传入正则
#limit参数
#限制传回的数量
#print soup.find_all("a",limit=2)#只传回两个
#recursive参数
#当调用find_all()方法时,会检索所有子孙tag,调用recursive参数可以只搜索子节点
#print soup.html.find_all('title',recursive=False)






#find( name , attrs , recursive , text , **kwargs )


#它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果


#find_parents()  find_parent()


#find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容


#find_next_siblings()  find_next_sibling()


#这2个方法通过 .next_siblings 属性对当 tag 的所有后面解析的兄弟 tag 节点进行迭代, find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点








#find_previous_siblings()  find_previous_sibling()


#这2个方法通过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点




#find_all_next()  find_next()


#这2个方法通过 .next_elements 属性对当前 tag 的之后的 tag 和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点




#find_all_previous() 和 find_previous()


#这2个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点


#css选择器
#我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list


#print soup.select('title')#通过标签名查找
#print soup.select('.sister')#通过类名查找,记得加句号
#print soup.select('#link1')#通过id名查找,记得加#


#组合查找,用空格 隔开
#print soup.select('p #link1')#p中的id为link1的内容
#print soup.select("head > title")#直接子标签查找


#属性查找    属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。


#print soup.select('a[class="sister"]')
#select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容
#print soup.select('title')[0].get_text()#第一种输出方法


#for title in soup.select('title'):#第二种输出方法
#    print title.text
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值