标签,返回结果也是返回出现的第一个匹配内容。
tag_li=soup.find('li')
#tag_li=soup.find(name="li")
printtype(tag_li)
printtag_li.div.string
搜索文本
如果我们只想根据文本内容来搜索的话,我们可以只传入文本参数:
search_for_text=soup.find(text='plants')
printtype(search_for_text)
返回的结果也是NavigableString对象。
根据正则表达式搜索
如下的一段HTML文本内容
ThebelowHTMLhastheinformationthathasemailids.
abc@example.com
xyz@example.com
foo@example.com
可以看到abc@example邮件地址并没有包括在任何标签内,这样就不能根据标签来找到邮件地址了。这个时候,我们可以使用正则表达式来进行匹配。
email_id_example="""
ThebelowHTMLhastheinformationthathasemailids.
abc@example.com
xyz@example.com
foo@example.com
"""
email_soup=BeautifulSoup(email_id_example,'lxml')
printemail_soup
#pattern="\w+@\w+\.\w+"
emailid_regexp=re.compile("\w+@\w+\.\w+")
first_email_id=email_soup.find(text=emailid_regexp)
printfirst_email_id
在使用正则表达式进行匹配时,如果有多个匹配项,也是先返回第一个。
根据标签属性值搜索
可以通过标签的属性值来搜索:
search_for_attribute=soup.find(id='primaryconsumers')
printsearch_for_attribute.li.div.string
根据标签属性值来搜索对大多数属性都是可用的,例如:id、style和title。
但是对以下两种情况会有不同:
我们不能再直接使用属性值来搜索了,而是得使用attrs参数来传递给find()函数。
根据自定义属性来搜索
在HTML5中是可以给标签添加自定义属性的,例如给标签添加属性。
如下代码所示,如果我们再像搜索id那样进行操作的话,会报错的,Python的变量不能包括-符号。
customattr="""
customattributeexample
"""
customsoup=BeautifulSoup(customattr,'lxml')
customsoup.find(data-custom="custom")
#SyntaxError:keywordcan'tbeanexpression
这个时候使用attrs属性值来传递一个字典类型作为参数进行搜索:
using_attrs=customsoup.find(attrs={'data-custom':'custom'})
printusing_attrs
基于CSS中的类进行搜索
对于CSS的类属性,由于在Python中class是个关键字,所以是不能当做标签属性参数传递的,这种情况下,就和自定义属性一样进行搜索。也是使用attrs属性,传递一个字典进行匹配。
除了使用attrs属性之外,还可以使用class_属性进行传递,这样与class区别开了,也不会导致错误。
css_class=soup.find(attrs={'class':'producerlist'})
css_class2=soup.find(class_="producerlist")
printcss_class
printcss_class2
使用自定义的函数搜索
可以给find()方法传递一个函数,这样就会根据函数定义的条件进行搜索。
函数应该返回true或者是false值。
defis_producers(tag):
returntag.has_attr('id')andtag.get('id')=='producers'
tag_producers=soup.find(is_producers)
printtag_producers.li.div.string
代码中定义了一个is_producers函数,它将检查标签是否具体id属性以及属性值是否等于producers,如果符合条件则返回true,否则返回false。
联合使用各种搜索方法
BeautifulSoup提供了各种搜索方法,同样,我们也可以联合使用这些方法来进行匹配,提高搜索的准确度。
combine_html="""
Exampleofptagwithclassidentical
Exampleofdivtagwithclassidentical
"""
combine_soup=BeautifulSoup(combine_html,'lxml')
identical_div=combine_soup.find("div",class_="identical")
printidentical_div
使用find_all()方法搜索
使用find()方法会从搜索结果中返回第一个匹配的内容,而find_all()方法则会返回所有匹配的项。
在find()方法中用到的过滤项,同样可以用在find_all()方法中。事实上,它们可以用到任何搜索方法中,例如:find_parents()和find_siblings()中。
#搜索所有class属性等于tertiaryconsumerlist的标签。
all_tertiaryconsumers=soup.find_all(class_='tertiaryconsumerlist')
printtype(all_tertiaryconsumers)
fortertiaryconsumersinall_tertiaryconsumers:
printtertiaryconsumers.div.string
find_all()方法为:
find_all(name,attrs,recursive,text,limit,**kwargs)
它的参数和find()方法有些类似,多个了limit参数。limit参数是用来限制结果数量的。而find()方法的limit就是1了。
同时,我们也能传递一个字符串列表的参数来搜索标签、标签属性值、自定义属性值和CSS类。
#搜索所有的div和li标签
div_li_tags=soup.find_all(["div","li"])
printdiv_li_tags
print
#搜索所有类属性是producerlist和primaryconsumerlist的标签
all_css_class=soup.find_all(class_=["producerlist","primaryconsumerlist"])
printall_css_class
print
搜索相关标签
一般情况下,我们可以使用find()和find_all()方法来搜索指定的标签,同时也能搜索其他与这些标签相关的感兴趣的标签。
搜索父标签
可以使用find_parent()或者find_parents()方法来搜索标签的父标签。
find_parent()方法将返回第一个匹配的内容,而find_parents()将返回所有匹配的内容,这一点与find()和find_all()方法类似。
#搜索父标签
primaryconsumers=soup.find_all(class_='primaryconsumerlist')
printlen(primaryconsumers)
#取父标签的第一个
primaryconsumer=primaryconsumers[0]
#搜索所有ul的父标签
parent_ul=primaryconsumer.find_parents('ul')
printlen(parent_ul)
#结果将包含父标签的所有内容
printparent_ul
print
#搜索,取第一个出现的父标签.有两种操作
immediateprimary_consumer_parent=primaryconsumer.find_parent()
#immediateprimary_consumer_parent=primaryconsumer.find_parent('ul')
printimmediateprimary_consumer_parent
搜索同级标签
BeautifulSoup还提供了搜索同级标签的功能。
使用函数find_next_siblings()函数能够搜索同一级的下一个所有标签,而find_next_sibling()函数能够搜索同一级的下一个标签。
producers=soup.find(id='producers')
next_siblings=producers.find_next_siblings()
printnext_siblings
同样,也可以使用find_previous_siblings()和find_previous_sibling()方法来搜索上一个同级的标签。
搜索下一个标签
使用find_next()方法将搜索下一个标签中第一个出现的,而find_next_all()将会返回所有下级的标签项。
#搜索下一级标签
first_div=soup.div
all_li_tags=first_div.find_all_next("li")
printall_li_tags
搜索上一个标签
与搜索下一个标签类似,使用find_previous()和find_all_previous()方法来搜索上一个标签。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。