我必须从this link解析阿德莱德乌鸦的玩家名称,因为我已经编写了这样的解析器
import requests
from bs4 import BeautifulSoup
href_val = requests.get("http://www.afl.com.au/news/teams?round=9")
soup1 = BeautifulSoup(href_val.content)
players_info_adel = soup1.find_all("ul", {"class" : "team1 team-adel"})
for li in players_info_adel:
player_names_adel = li.find_all("li", {"class" : "player"})
#print player_names_adel
#print player_names_adel
for span in player_names_adel:
if span.find(text = True):
text = ''.join(span.find(text = True))
text1 = text.encode('ascii')
print text
但是每当我运行这段代码时,我总会得到一堆"\n"
而不是名字。我该怎么做才能得到球员的名字?
答案 0 :(得分:1)
您不希望循环超过每个玩家<li>
元素;第一个元素是一个文本节点,其中只有一个换行符。最好使用Tag.get_text()
来获取元素中的所有文本。
使用CSS选择器简化代码:
for player in soup1.select('ul.team1 li.player'):
text = player.get_text().strip()
print text
这包括球员号码;您可以使用以下方法分隔此号码和玩家名称:
number, name = player.span.get_text().strip(), player.span.next_sibling.strip()
代替。
演示:
>>> import requests
>>> from bs4 import BeautifulSoup
>>> href_val = requests.get("http://www.afl.com.au/news/teams?round=9")
>>> soup1 = BeautifulSoup(href_val.content)
>>> for player in soup1.select('ul.team1 li.player'):
... text = player.get_text().strip()
... print text
...
24 Sam Jacobs
32 Patrick Dangerfield
26 Richard Douglas
41 Kyle Hartigan
25 Ben Rutten
16 Luke Brown
33 Brodie Smith
# .. etc ..
>>> for player in soup1.select('ul.team1 li.player'):
... number, name = player.span.get_text().strip(), player.span.next_sibling.strip()
... print name
...
Sam Jacobs
Patrick Dangerfield
Richard Douglas
Kyle Hartigan
Ben Rutten
Luke Brown
Brodie Smith
# ... etc ...