BeautifulSoup 的 find聚凹、find_all呵恢、select 方法
from bs4 import BeautifulSoup
lxml 以lxml形式解析html呵哨,例:BeautifulSoup(html,'lxml') # 注:html5lib 容錯率最高
find 返回找到的第一個標簽
find_all 以list的形式返回找到的所有標簽
limit 指定返回的標簽個數(shù)
attrs 將標簽屬性放到一個字典中
string 獲取標簽下的非標簽字符串(值), 返回字符串
strings 獲取標簽下的所有非標簽字符串刹前, 返回生成器毛嫉。
stripped_strings 獲取標簽下的所有非標簽字符串袱饭,并剔除空白字符川无,返回生成器。
get_text # 獲取標簽下的所有非標簽字符串,返回字符串格式
contents虑乖、children都是返回某個標簽下的直接子元素懦趋,包含字符串。 contents 返回一個列表疹味,children 返回一個生成器
select 方法和find_all極其相似
定義一個html仅叫,并使用BeautifulSoup的lxml解析
from bs4 import BeautifulSoup
html = '''
<table>
<tr class='a1'>
<td>職位名稱</td>
<td>職位類別</td>
<td>時間</td>
</tr>
<tr class='a1'>
<td><a id='test' class='test' target='_blank' >職位一</a></td>
<td>類別一</td>
<td>時間1</td>
</tr>
<tr class='a2'>
<td><a id='test' class='test' target='_blank' >職位二</a></td>
<td>類別二</td>
<td>時間2</td>
</tr class='a3'>
<tr>
<td><a id='test' class='test' target='_blank' >職位3</a></td>
<td>類別3</td>
<td>時間3</td>
</tr>
</table>
<div>
這是一個div
<p>
<!-- 這是一個注釋 -->
</p>
</div>
'''
soup = BeautifulSoup(html,'lxml') # 解析html
find_all()
獲取所有的tr標簽
find 返回找到的第一個標簽,find_all以list的形式返回找到的所有標簽
trs = soup.find_all('tr') # 返回列表
n=1
for i in trs: print('第{}個tr標簽:'.format(n)) print(i)
n+=1
獲取第二個tr標簽
limit 可指定返回的標簽數(shù)量
trs = soup.find_all('tr',limit=2)[1] # 從列表中獲取第二個元素糙捺,limit 獲取標簽個數(shù)
print(trs)
獲取class='a1'的tr標簽
方法一: class_
trs = soup.find_all('tr',class_='a1')
n=1
for i in trs: print('第{}個class=''a1''的tr標簽:'.format(n)) print(i)
n+=1
方法二:attrs 將標簽屬性放到一個字典中
trs = soup.find_all('tr',attrs={'class':'a1'})
n=1
for i in trs: print('第{}個class=''a1''的tr標簽:'.format(n)) print(i)
n+=1
提取所有id='test'且class='test'的a標簽
方法一:class_
alist = soup.find_all('a',id='test',class_='test')
n=1
for i in alist: print('第{}個id=''test''且class=''test''的a標簽:'.format(n)) print(i)
n+=1
方法二:attrs
alist = soup.find_all('a',attrs={'id':'test','class':'test'})
n=1
for i in alist: print('第{}個id=''test''且class=''test''的a標簽:'.format(n)) print(i)
n+=1
獲取所有a標簽的href屬性**
alist = soup.find_all('a') #方法一:通過下標獲取
for a in alist:
href = a['href'] print(href) #方法二: 通過attrs獲取
for a in alist:
href = a.attrs['href'] print(href)
獲取所有的職位信息(所有文本信息)
string 獲取標簽下的非標簽字符串(值), 返回字符串
注:第一個tr為標題信息诫咱,不獲取。從第二個tr開始獲取洪灯。
trs = soup.find_all('tr')[1:]
movies = [] for tr in trs:
move = {}
tds = tr.find_all('td')
move['td1'] = tds[0].string # string 取td的值
move['td2'] = tds[1].string
move['td3'] = tds[2].string
movies.append(move) print(movies)
獲取所有非標記性字符
strings 獲取標簽下的所有非標簽字符串坎缭, 返回生成器。
trs = soup.find_all('tr')[1:] for tr in trs:
infos = list(tr.strings) # 獲取所有非標記性字符签钩,包含換行掏呼、空格
print(infos)
獲取所有非空字符
stripped_strings 獲取標簽下的所有非標簽字符串,并剔除空白字符边臼,返回生成器哄尔。
trs = soup.find_all('tr')[1:] for tr in trs:
infos = list(tr.stripped_strings) # 獲取所有非空字符,不包含換行柠并、空格
print(infos)
# stripped_strings 獲取所有職位信息
trs = soup.find_all('tr')[1:]
movies = [] for tr in trs:
move = {}
infos = list(tr.stripped_strings)
move['職位'] = infos[0]
move['類別'] = infos[1]
move['時間'] = infos[2]
movies.append(move) print(movies)
get_text 獲取所有職位信息
get_text 獲取標簽下的所有非標簽字符串,返回字符串格式
trs = soup.find_all('tr')[1]
text = trs.get_text() # 返回字符串格式
print(text)
select()
獲取所有tr標簽
trs = soup.select('tr') for i in trs: print('tr標簽:',i)
獲取第二個tr標簽
trs = soup.select('tr')[1] print(trs)
獲取所有class="al"的tr標簽
# 方法一:
trs = soup.select('tr.a1') # tr標簽的class屬性
for i in trs: print(i) # 方法二:
trs = soup.select('tr[class="a1"]') # tr標簽的class屬性
for i in trs: print(i)
提取所有a標簽的href屬性
# 方法一:
a = soup.select('a') for i in a: print(i['href']) # 方法二:
a = soup.select('a') for i in a: print(i.attrs['href'])
獲取所有的職位信息
trs = soup.select('tr') for i in trs: print(list(i.stripped_strings))