XML 解析器语法错误

发布于 2024-09-18 08:04:26 字数 2376 浏览 2 评论 0原文

因此,我正在使用与 Flickr API 进行通信的代码块。

我在 xml.parsers.expat.ExpatError (如下)中收到“语法错误”。现在我不明白为什么Python 模块会出现语法错误。

我在 SO 上看到了另一个类似的问题,关于 Wikipedia API,它似乎返回 HTML 而不是 XML。 Flickr API 返回 XML;当 Flickr 不应该响应时(例如 flickr.galleries.addPhoto),我也会遇到同样的错误

CODE:

def _dopost(method, auth=False, **params):
    #uncomment to check you aren't killing the flickr server
    #print "***** do post %s" % method

    params = _prepare_params(params)
    url = '%s%s/%s' % (HOST, API, _get_auth_url_suffix(method, auth, params))
    payload = 'api_key=%s&method=%s&%s'% \
          (API_KEY, method, urlencode(params))

    #another useful debug print statement
    #print url
    #print payload

    return _get_data(minidom.parse(urlopen(url, payload)))

TRACEBACK:< /strong>

Traceback (most recent call last):
  File "TESTING.py", line 30, in <module>
    flickr.galleries_create('test_title', 'test_descriptionn goes here.')
  File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1006, in galleries_create
    primary_photo_id=primary_photo_id)
  File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1066, in _dopost
    return _get_data(minidom.parse(urlopen(url, payload)))
  File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
    return expatbuilder.parse(file)
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: syntax error: line 1, column 62

(代码来自 http://code.google.com/p/flickrpy/ 在新的 BSD 许可证下)

更新:

print urlopen(url, Payload) == >

执行 urlopen(url, Payload).read() 返回很难在终端中读取的 HTML :P 但我设法使输出“您尚未登录。”
奇怪的是,Flickr 不应该在此处返回任何内容,或者如果权限有问题,它应该返回一个 99: 用户未登录/权限不足 错误,就像使用 GET 函数一样(这我希望是有效的 XML)。

我已登录 Flickr(在浏览器中),并且该程序已使用 delete 权限进行了正确身份验证(很危险,但我想避免权限问题。)

So I'm working with a block of code which communicates with the Flickr API.

I'm getting a 'syntax error' in xml.parsers.expat.ExpatError (below). Now I can't figure out how it'd be a syntax error in a Python module.

I saw another similar question on SO regarding the Wikipedia API which seemed to return HTML intead of XML. Flickr API returns XML; and I'm also getting the same error when there shouldn't be a response from Flickr (such as flickr.galleries.addPhoto)

CODE:

def _dopost(method, auth=False, **params):
    #uncomment to check you aren't killing the flickr server
    #print "***** do post %s" % method

    params = _prepare_params(params)
    url = '%s%s/%s' % (HOST, API, _get_auth_url_suffix(method, auth, params))
    payload = 'api_key=%s&method=%s&%s'% \
          (API_KEY, method, urlencode(params))

    #another useful debug print statement
    #print url
    #print payload

    return _get_data(minidom.parse(urlopen(url, payload)))

TRACEBACK:

Traceback (most recent call last):
  File "TESTING.py", line 30, in <module>
    flickr.galleries_create('test_title', 'test_descriptionn goes here.')
  File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1006, in galleries_create
    primary_photo_id=primary_photo_id)
  File "/home/vlad/Documents/Computers/Programming/LEARNING/curatr/flickr.py", line 1066, in _dopost
    return _get_data(minidom.parse(urlopen(url, payload)))
  File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
    return expatbuilder.parse(file)
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: syntax error: line 1, column 62

(Code from http://code.google.com/p/flickrpy/ under New BSD licence)

UPDATE:

print urlopen(url, payload) == <addinfourl at 43340936 whose fp = <socket._fileobject object at 0x29400d0>>

Doing a urlopen(url, payload).read() returns HTML which is hard to read in a terminal :P but I managed to make out a 'You are not signed in.'
The strange part is that Flickr shouldn't return anything here, or if permissions are a problem, it should return a 99: User not logged in / Insufficient permissions error as it does with the GET function (which I'd expect would be in valid XML).

I'm signed in to Flickr (in the browser) and the program is properly authenticated with delete permissions (dangerous, but I wanted to avoid permission problems.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷不放纵 2024-09-25 08:04:26

SyntaxError 通常表示 Python 语法中的错误,但我认为这里 expatbuilder 重载了它以表示 XML 语法错误。在其周围放置一个 try:except 块,并打印出 payload 的内容并找出第一行的问题所在。

我的猜测是,flickr 出于某种原因拒绝了您的请求,并返回一条纯文本错误消息,该消息在第 62 列有一个无效的 xml 字符,但它可能是任意数量的字符。您可能想在解析 http 状态代码之前检查它。

另外,这个方法被称为_dopost,但你似乎实际上正在发送一个http GET,这有点奇怪。也许这就是它失败的原因。

SyntaxError normally means an error in Python syntax, but I think here that expatbuilder is overloading it to mean an XML syntax error. Put a try:except block around it, and print out the contents of payload and to work out what's wrong with the first line of it.

My guess would be that flickr is rejecting your request for some reason and giving back a plain-text error message, which has an invalid xml character at column 62, but it could be any number of things. You probably want to check the http status code before parsing it.

Also, it's a bit strange this method is called _dopost but you seem to actually be sending an http GET. Perhaps that's why it's failing.

明月夜 2024-09-25 08:04:26

这似乎解决了我的问题:

url = '%s%s/?api_key=%s&method=%s&%s'% \
      (HOST, API, API_KEY, method, _get_auth_url_suffix(method, auth, params))
payload = '%s' % (urlencode(params))

API 密钥和方法似乎必须位于 URL 中,而不是负载中。 (或者也许只需要一个人在那里,但无论如何,它有效:-)

This seems to fix my problem:

url = '%s%s/?api_key=%s&method=%s&%s'% \
      (HOST, API, API_KEY, method, _get_auth_url_suffix(method, auth, params))
payload = '%s' % (urlencode(params))

It seems that the API key and method had to be in the URL not in the payload. (Or maybe only one needed to be there, but anyways, it works :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文