- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 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">
但是
尝试用 Expat 解析它产生错误 not well-formed (invalid token)
。
W3C's online validation service报告 399 条错误和 121 条警告。
我尝试使用 -xml
选项在我的 Linux 系统上运行 HTML tidy(称为 tidy
),但 tidy 报告了 56 条警告和 117 条错误,并且无法恢复良好的 XML 文件。错误如下所示:
line 409 column 122 - Warning: unescaped & or unknown entity "&role"
...
line 409 column 172 - Warning: unescaped & or unknown entity "&tabSeq"
...
line 1208 column 65 - Error: unexpected </td> in <br>
line 1209 column 57 - Error: unexpected </tr> in <br>
line 1210 column 49 - Error: unexpected </table> in <br>
但是当我检查输入时,“未知实体”似乎是正确引用的 URL 的一部分,所以我不知道是某处缺少双引号还是什么。
我知道那里有东西可以解析这些东西,因为 Firefox 和 w3m 都显示了一些合理的东西。 什么工具可以修复不兼容的 HTML,以便我可以使用 Expat 对其进行解析?
最佳答案
他们在计分框上使用了某种 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>
可能更容易抓取 Yahoo's NFL scoreboard ,在我看来......事实上,去试试吧。
编辑:以您的问题为借口开始学习 BeautifulSoup。 Alex Martelli 一直对其赞不绝口,所以我认为它值得一试 -- 伙计,我印象深刻。
无论如何,我能够从 Yahoo!记分牌,像这样:
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)
总之,一个让你跳的木桩。祝你好运。
关于html - 如何修复不兼容的 HTML 以便 Expat 解析它(htmltidy 不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1814731/
我很难找到为什么在共享库(它是 firefox xpcom 组件)中链接静态库(包含 expat 的 XMP)时出现未解析的外部符号。 我在 Windows 下使用 Windows 编译器(标志:“-
当我从我的 XML 文档中打印出信息时,我在每一行之前得到了 ��。这是我的 XML 文档。 Tove Jani Reminder Don't forget me this weekend! 这
我找不到任何关于 expat-parser 在 XML 标签方面支持和不支持的文档。我知道在一般情况下,expat 让事件处理程序(由用户实现)处理标签,但我特别想知道 expat 是否支持任何类型的
我正在尝试安装node-expat。我已经尝试了我能找到的所有建议。运行 npm install node-expat 时,我没有收到任何构建错误。我已经放入了所有依赖项和构建工具包,尝试了 npm
我的项目依赖于 expat。我的问题是我无法链接到 expat。我使用的是Ubuntu(debian环境),需要使用cmake来构建makefile。 我尝试下载源代码并在我的 CMakeLists.
基于树的解析器:这种解析器把 XML 文档转换为树型结构。它分析整篇文档,并提供了对树中元素的访问,例如文档对象模型 (DOM)。 基于时间的解析器:将 XML 文档视为一系列的事件。当某个具体的
我刚开始使用 expat。我试图弄清楚 XML_SetUserData 的用途。在我的例子中,我有一个 A 类对象列表,我需要在解析 XML 流时填充这个列表。 到目前为止,我计划使用单例来表示这个单
我已经用 expat 解析器注册了三个处理程序:- 开始-结尾- 文字 然后从主程序中,我读取了 xml 文件,对其进行缓冲并调用 XML_Parse API。像这样: try { if( ! XML
我正在使用 expat 编写 xml 解析器。刚看到这个: XML_SetElementHandler XML_Parser parser, XML_StartElementHan
我正在将 bluez 交叉编译到我的板上,但无法编译 dbus。 配置时,出现此错误消息: checking for XML_ParserCreate_MM in -lexpat... no conf
我正在使用 MingW 在 Windows 上编写 C 程序,并希望使用 EXPAT XML 库。我想静态编译我的程序,所以我需要静态 .a 库。 有什么方法可以将 EXPAT 编译成 Windows
我有一个二进制流,其中包含串联的 XML 文档。使用如下调用以任意大小的 block 处理流: int expat_status = XML_Parse(parser->expat, buffer,
这是我第一次使用外部 header ,我对外籍人士一无所知。Expat 官方网站对于初学者来说没什么用。我需要使用 expat.h 用 C 语言编译 XML 解析器代码。我已经下载了 expat 文件
我正在使用 expat 读取 xml 文件。我想从 xml 文件中指定的配置中填充我的一些类成员变量。我已经定义了我的 startelement 处理程序, void Start(void *data
Expat 有一个用于 lib 的 .la 文件。我正在尝试使用 XCode 链接它。 我所做的是将目标的“其他链接标志”设置为 libexpat.la,并将“库搜索路径设置为 .la 文件所在的位置
我对 expat 的 XML 验证有疑问。我没有找到可以帮助我验证 xml 文档的文档或 wiki。 是否可以使用 libexpat 和 C 程序通过 xml 模式(xsd 文件)验证我的 XML 文
https://github.com/TooTallNate/node-expat/tree/gyp和node-gyp用于编译它。生成了node_expat.node。在代码中引用时出现以下错误: m
我按照these instructions制作了本地wsdl。 我的本地WSDL中的一些行: 当我像这样使用python时: from SOAPpy import WSDL serv
import xml.parsers.expat def start_element(name, attrs): print('Start element:', name, attrs) de
我正在使用 XML-Simple 和 XML-SAX-Expat 来解析包含外部实体的文档。我这样声明我的实体: 我指的是同一目录下的另一个文件。 但是,它正在我的当前工作目录中搜索该文件,而不是根
我是一名优秀的程序员,十分优秀!