day_03 2
基本使用
html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="sister"><b>$37</b></p> <p class="story" id="p">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" >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> """
find:找第一个
find_ all:找所有
标签查找与属性查找:
name 属性匹配
name 标签名
attrs 属性查找匹配
name 标签名
attrs 属性查找匹配
text 文本匹配
标签:
-字符串过滤器 字符串全局匹配
标签:
-字符串过滤器 字符串全局匹配
-正则过滤器
re模块匹配
re模块匹配
-列表过滤器
列表内的数据匹配
-bool过滤器
True匹配
True匹配
-方法过滤器
用于一些要的属性以及不需要的属性查找。
用于一些要的属性以及不需要的属性查找。
属性:
- class_
- class_
- id
html_doc = """ <html><head><title>The Dormouse's story</title></head><body><p class="sister"><b>$37</b></p><p class="story" id="p">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" >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> """ from bs4 import BeautifulSoup soup=BeautifulSoup(html_doc,'lxml') ''' 字符串过滤器 ''' p=soup.find(name='p') p_s=soup.find_all(name='p') print(p) print(p_s) p=soup.find(name='p',attrs={"id":"p"}) print(p) tag=soup.find(name='a',attrs={"class":"sister"},text="Elsie") print(tag) ''' 正则过滤器 re模块匹配 ''' import re a=soup.find(name=re.compile('a')) print(a) a_s=soup.find_all(name=re.compile('a')) print(a_s) a=soup.find(attrs={"id":re.compile('link')}) print(a) print(soup.find(name=['a','p''html',re.compile('a')])) print(soup.find_all(name=['a','p','html',re.compile('a')])) print(soup.find(name=True,attrs={"id":True})) def have_id_not_class(tag): if tag.name=='p' and tag.has_attr("id") and not tag.has_attr("class"): return tag print(soup.find_all(name=have_id_not_class)) a=soup.find(id='link2') print(a) p=soup.find(class_='sister') print(p)
html_doc = """
<html><head><title>The Dormouse's story</title></head><body><p class="sister"><b>$37</b></p><p class="story" id="p">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" >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>
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html_doc,'lxml')
'''
字符串过滤器
'''
p=soup.find(name='p')
p_s=soup.find_all(name='p')
print(p)
print(p_s)
p=soup.find(name='p',attrs={"id":"p"})
print(p)
tag=soup.find(name='a',attrs={"class":"sister"},text="Elsie")
print(tag)
'''
正则过滤器
re模块匹配
'''
import re
a=soup.find(name=re.compile('a'))
print(a)
a_s=soup.find_all(name=re.compile('a'))
print(a_s)
a=soup.find(attrs={"id":re.compile('link')})
print(a)
print(soup.find(name=['a','p''html',re.compile('a')]))
print(soup.find_all(name=['a','p','html',re.compile('a')]))
print(soup.find(name=True,attrs={"id":True}))
def have_id_not_class(tag):
if tag.name=='p' and tag.has_attr("id") and not tag.has_attr("class"):
return tag
print(soup.find_all(name=have_id_not_class))
a=soup.find(id='link2')
print(a)
p=soup.find(class_='sister')
print(p)