无法访问<a>美丽汤中标签中的字符串



我正试图使用以下代码从html表中获取标签:

url = "https://www.worldfootball.net/report/premier-league-2007-2008-arsenal-fc-fulham-fc/"
url_content=requests.get(url).content
match_content = BeautifulSoup(url_content,'html.parser')
tag = match_content.find_all(class_="standard_tabelle")
#Get teams
teams = tag[0].find_all("a")
#Iterate through goals
for a in tag[1].select("tr"):
b = a.select_one("a")
print(b)

如果我"打印"我得到的结果:

None
<a href="/player_summary/david-healy/" title="David Healy">David Healy</a>
<a href="/player_summary/robin-van-persie/" title="Robin van Persie">Robin van Persie</a>
<a href="/player_summary/aliaksandr-hleb/" title="Aliaksandr Hleb">Aliaksandr Hleb</a>

但是如果我尝试打印"b.string",我会得到以下错误:

AttributeError:"NoneType"对象没有属性"string">

我很感激我的代码可能不是最健壮的,但我无法理解为什么它不会从每个结果中获取字符串。如有任何帮助,我们将不胜感激。非常感谢

tag[1].select("tr")返回的第一个<tr>中没有<a>。当print(b)打印None时可以看到。但是,您尝试使用它来获取string属性,但这不起作用。检查b是否真的包含一些东西:

if b:
print(b.string)

最新更新