无法使用BeautifulSoup获取标记的值

时间:2012-10-06 20:26:25

标签: python web-scraping beautifulsoup

我刚刚开始使用BS4,我似乎无法找到为什么我无法提取下表中的文字 - > http://pastebin.com/MCQC7wLY

这是我的代码:

    for team in soup.find_all('tr'):
    print team.a.string

我收到以下错误

  

AttributeError:'NoneType'对象没有属性'string'

我也尝试了其他类似的东西,比如

for team in soup.find_all('tr'):
    print team.find('a').string

但我总是得到同样的错误。

这就是team.find('a')返回

<a href="/entry/688922/event-history/7/">FC Lasne</a>

我想提取“FC Lasne”

这让我很生气,因为通常我只是找到('a')。字符串及其正常工作

我该怎么办?

由于

1 个答案:

答案 0 :(得分:0)

示例中的第一个tr中没有任何a个标记。

您可以忽略没有链接的任何tr

for team in soup.find_all('tr'):
    link = team.find('a')
    if link == null:
       continue
    print link.string

虽然你可以这样做:

soup.find_all('a')