I am trying to grab the tags from html table using the code below:
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)
If I 'print' the result I get:
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>
but if I try to print 'b.string' I get the following error:
AttributeError: 'NoneType' object has no attribute 'string'
I appreciate my code probably isn't the most robust but I can't fathom why it won't grab the string from each result. Any help would be appreciated. Many thanks
答案 0 :(得分:0)
由<tr>
返回的第一个tag[1].select("tr")
中没有<a>
。您可以看到print(b)
打印None
时。但是,然后您尝试使用该属性获取string
属性,该属性不起作用。检查b
是否实际包含以下内容:
if b:
print(b.string)