如何从段落中的多个跨度提取数据

时间:2019-02-26 23:44:34

标签: python-3.x beautifulsoup python-requests

`#如何使用美丽汤从鹰嘴中提取鹰

playerTeam = soup.find_all('p',attrs={'class':'player-details'})

for teams in playerTeam:
    team = teams.get('span')
    playerTeam.append(team)

输出:

<p class="player-details"><span>#10</span><span>G</span><span>Hawks</span></p>

3 个答案:

答案 0 :(得分:0)

尝试以下代码,您可以使用re模块搜索特定文本。

import bs4
import re
html_doc="""<html><p class="player-details">
<span>#10</span>
<span>G</span>
<span>Hawks</span>
</p></html>"""
soup = bs4.BeautifulSoup(html_doc, 'html.parser')
find_p=soup.find('p' ,class_='player-details')
findspan=find_p.find('span' ,text=re.compile("Hawks"))
print(findspan.text)

输出:

Hawks

或者,如果要提取跨度中的所有值,请尝试此操作。

import bs4
html_doc="""<html><p class="player-details">
<span>#10</span>
<span>G</span>
<span>Hawks</span>
</p><p class="player-details">
<span>#20</span>
<span>G</span>
<span>Hawks</span>
</p></html>"""
soup = bs4.BeautifulSoup(html_doc, 'html.parser')
find_p=soup.find_all('p' ,class_='player-details')

for p in find_p:
 findspan=p.find_all('span')
 for sp in findspan:
   print(sp.text)

输出:

#10
G
Hawks
#20
G
Hawks

让我知道您是否正在照顾。

答案 1 :(得分:0)

我相信您正在寻找这样的东西:

import bs4

html_doc="""<html>
<p class="player-details"><span>#10</span><span>G</span><span>Hawks</span></p>
<p class="player-details"><span>#11</span><span>H</span><span>Nicks</span></p>
</html>"""

soup = bs4.BeautifulSoup(html_doc, 'html.parser')
sel_p=soup.select('p.player-details')
for i in find_p:
     print(i.text)

输出:

#10
G
Hawks


#11
H
Nicks

答案 2 :(得分:0)

playerTeam_Cur = []
playerTeam = soup.find_all('p',attrs={'class':'player-details'})
for i in range(len(playerList)):
    team = playerTeam[i].find_all('span')
    playerTeam_Cur.append(team[2].text)

for x in playerTeam_Cur:
print(x)

Output:    
Hawks
Thunder
Heat