为什么这个美丽组代码输出“无”?

2024-05-17 19:51:50 发布

您现在位置:Python中文网/ 问答频道 /正文

import urllib2
from BeautifulSoup import BeautifulSoup

contenturl = "http://espnfc.com/tables/_/league/esp.1/spanish-la-liga?cc=5901"
soup = BeautifulSoup(urllib2.urlopen(contenturl).read())

table = soup.find('div id', attrs={'class': 'content'})

rows = soup.findAll('tr')
for tr in rows:
    cols = tr.findAll('td')
    for td in cols:
        text = td.find(text=True)
        print text,  
    print

我得到了:(注意,这只是我想要的东西的一小部分,这是一个足球联赛的排名)

^{pr2}$

我的问题是, 为什么每个字后面都有一个“无”?有没有办法让它停止那样做?在


Tags: textinimportforfindurllib2trrows
2条回答

如果你在网站上注意到,一些信息之间有空格,这些信息包含在每个td中。在

您可能会注意到所有的空间都有一个宽度。所以,你可以这样做:

cols = tr.findAll('td', width=None)

如果您决定在任何阶段交换到BeautifulGroup 4,请使用:

^{pr2}$

当一个元素有多个子元素时,如The Docs中所示,则发生None

去除None的最简单方法如下:

for tr in rows:
    cols = tr.findAll('td')
    for td in cols:
        text = td.find(text=True)
        if text is not None:
            print text,  
    print  

它将检查text = None如果是,则不会打印它

相关问题 更多 >