我正在尝试从http://www.nfl.com/scores中抓取信息(特别是,找出游戏何时结束,这样我的计算机就可以停止录制)。我可以很容易地下载HTML,它声称符合标准:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">但
not well-formed (invalid token)。tidy选项来运行HTML (只称为-xml ),但是XML报告了56个警告和117个错误,并且无法恢复一个好的-xml文件。这些错误如下所示:
第409行第122栏-警告:未转义或未知实体“&角色”.第409行第172栏-警告:未转义或未知实体"&tabSeq“第1208行第65列-错误:第1209行第57列中的意外-错误:第1210行中的意外-第49列-错误:
但是当我检查输入时,“未知实体”似乎是一个正确引用的URL的一部分,所以我不知道在什么地方缺少双引号什么的。我知道有一些东西可以解析这些东西,因为火狐和w3m都显示了一些合理的东西。什么工具可以修复不兼容的HTML,这样我就可以用Expat?解析它。
发布于 2009-11-29 15:11:02
在nfl.com顶部有一个基于Flash的自动更新记分板。对其网络流量的一些监测发现:
http://www.nfl.com/liveupdate/scorestrip/ss.xml
这可能比HTML记分板更容易解析。
发布于 2009-11-29 05:48:54
他们在得分框上使用了某种Javascript,所以您必须玩更聪明的把戏(行换我的):
/* box of awesome */
// iscurrentweek ? true;
(new nfl.scores.Game('2009112905','54635',{state:'pre',container:'scorebox-2009112905',
wrapper:'sb-wrapper-2009112905',template:($('scorebox-2009112905').innerHTML),homeabbr:'NYJ',
awayabbr:'CAR'}));然而,为了回答您的问题,BeautifulSoup解析它(似乎)很好:
fp = urlopen("http://www.nfl.com/scores")
data = ""
while 1:
r = fp.read()
if not r:
break
data += r
fp.close()
soup = BeautifulSoup(data)
print soup.contents[2].contents[1].contents[1]产出:
<title>NFL Scores: 2009 - Week 12</title>在我的雅虎NFL记分板事实中,可能更容易刮掉opinion...in,去尝试一下。
编辑:用你的问题作为借口来学习BeautifulSoup。亚历克斯·马泰利一直在赞美它,所以我觉得值得一试--伙计,我印象深刻吗?
不管怎么说,我可以做一个基本的分数刮刀从雅虎!记分板,像这样:
def main():
soup = BeautifulSoup(YAHOO_SCOREBOARD)
on_first_team = True
scores = []
hold = None
# Iterate the tr that contains a team's box score
for item in soup(name="tr", attrs={"align": "center", "class": "ysptblclbg5"}):
# Easy
team = item.b.a.string
# Get the box scores since we're industrious
boxscore = []
for quarter in item(name="td", attrs={"class": "yspscores"}):
boxscore.append(int(quarter.string))
# Final score
sub = item(name="span", attrs={"class": "yspscores"})[0]
if sub.b:
# Winning score
final = int(sub.b.string)
else:
data = sub.string.replace(" ", "")
if ":" in data:
# Catch TV: XXX and 0:00pm ET
final = None
else:
try: final = int(data)
except: final = None
if on_first_team:
hold = { team : (boxscore, final) }
on_first_team = False
else:
hold[team] = (boxscore, final)
scores.append(hold)
on_first_team = True
for game in scores:
print "--- Game ---"
for team in game:
print team, game[team]我会在周日对此进行调整,看看它是如何运作的,因为它真的很粗糙。下面是它现在输出的内容:
--- Game ---
Green Bay ([0, 13, 14, 7], 34)
Detroit ([7, 0, 0, 5], 12)
--- Game ---
Oakland ([0, 0, 7, 0], 7)
Dallas ([3, 14, 0, 7], 24)你看,我也帮你记了分数.对于一个还没有发生的游戏,我们得到:
--- Game ---
Washington ([], None)
Philadelphia ([], None)不管怎么说,这是你的跳伞。祝好运。
发布于 2009-11-29 20:11:09
调查塔格汤。如果您想以Java中的DOM树或SAX流结束,这就是票证。如果你只想提取特定的信息,美丽的汤是一件美丽的东西。
https://stackoverflow.com/questions/1814731
复制相似问题