使用 Python 的维基百科

问题描述:

我有这个非常简单的python代码来读取维基百科api的xml:

I have this very simple python code to read xml for the wikipedia api:

import urllib
from xml.dom import minidom

usock = urllib.urlopen("http://en.wikipedia.org/w/api.php?action=query&titles=Fractal&prop=links&pllimit=500")
xmldoc=minidom.parse(usock)
usock.close()
print xmldoc.toxml() 

但此代码返回时出现以下错误:

But this code returns with these errors:

Traceback (most recent call last):
  File "/home/user/workspace/wikipediafoundations/src/list.py", line 5, in <module><br>
    xmldoc=minidom.parse(usock)<br>
  File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse<br>
    return expatbuilder.parse(file)<br>
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 928, in parse<br>
    result = builder.parseFile(file)<br>
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile<br>
    parser.Parse(buffer, 0)<br>
xml.parsers.expat.ExpatError: syntax error: line 1, column 62<br>

我不知道,因为我只是在学习 python.有没有办法获得更详细的错误?有谁知道解决方案?另外,请推荐一种更好的语言来执行此操作.

I have no clue as I just learning python. Is there a way to get an error with more detail? Does anyone know the solution? Also, please recommend a better language to do this in.

谢谢,
文卡特·拉奥

Thank You,
Venkat Rao

您请求的 URL 是将返回的 XML 的 HTML 表示:

The URL you're requesting is an HTML representation of the XML that would be returned:

http://en.wikipedia.org/w/api.php?action=query&titles=Fractal&prop=links&pllimit=500

所以 XML 解析器失败了.您可以通过在浏览器中粘贴以上内容来查看这一点.尝试在末尾添加 format=xml:

So the XML parser fails. You can see this by pasting the above in a browser. Try adding a format=xml at the end:

http://en.wikipedia.org/w/api.php?action=query&titles=Fractal&prop=links&pllimit=500&format=xml

如链接页面所述: