我正在尝试从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
给你:
输出:
其思想是先收集原始数据,然后编写逻辑来显示数据(以任何方式)。你知道吗
相关问题 更多 >
编程相关推荐