如何在python中分隔表中的每个tr?

2024-10-06 09:20:23 发布

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

我正在尝试从http://www.livescore.co.uk/worldcup/tables/.I解析表。我在管理输出时遇到了问题。我只想在输出中显示文本,还想在显示所有td之后在每个tr上加一个分隔符。我是初学者,我正在尝试学习。所以呢有人能告诉我在做什么吗错了,有吗建议?你知道吗

from BeautifulSoup import BeautifulSoup
import urllib2

pageSource=urllib2.urlopen('http://www.livescore.com/worldcup/tables/').read()

soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"class":"league-wc table bh"} )
results=[]
for table in alltables:
    rows = table.findAll('tr')
    lines=[]
    for tr in rows[1:]:
        cols = tr.findAll('td')
        for td in cols:

            text=td.renderContents().strip('\n')

            lines.append(text)




    text_table='\n'.join(lines) 
    print text_table

输出:

<a href="/worldcup/team-brazil/">Brazil</a> 
0
0
0
0
0
0
0
0
1
 <a href="/worldcup/team-cameroon/">Cameroon</a> 
0
0
0
0
0
0
0
0
1
 <a href="/worldcup/team-croatia/">Croatia</a> 
0
0
0
0
0
0
0
0
1
 <a href="/worldcup/team-mexico/">Mexico</a> 
0
0
0
0
0
0
0
0
 ....similar

我的愿望输出:

1,brazil,0,0,0,0,0,0,0,0,0,0
2,cameroon,0,0,0,0,0,0,0,0,0,0
3,craotia,0,0,0,0,0,0,0,0,0,0
4,Meico,0,0,0,0,0,0,0,0,0,0

Tags: textinhttpforwwwtabletrteam
1条回答
网友
1楼 · 发布于 2024-10-06 09:20:23

给你:

from BeautifulSoup import BeautifulSoup
import urllib2

pageSource=urllib2.urlopen('http://www.livescore.com/worldcup/tables/').read()

soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"class":"league-wc table bh"} )

results=[]
for table in alltables:
    rows = table.findAll('tr')
    _table = []
    for tr in rows[1:]:
        _row = []
        cols = tr.findAll('td')
        for td in cols:
            if td.findAll('a'):
                text=td.a.renderContents().strip()
            else:
                text=td.renderContents().strip()
            _row.append(text)
        _table.append(_row)
    results.append(_table)


# print results
index = 1
for table in results:
    for row in table:
        print ','.join([str(index)] + row[1:])
        index += 1

输出:

1,Brazil,0,0,0,0,0,0,0,0
2,Cameroon,0,0,0,0,0,0,0,0
3,Croatia,0,0,0,0,0,0,0,0
4,Mexico,0,0,0,0,0,0,0,0
5,Australia,0,0,0,0,0,0,0,0
6,Chile,0,0,0,0,0,0,0,0
...

其思想是先收集原始数据,然后编写逻辑来显示数据(以任何方式)。你知道吗

相关问题 更多 >