0

我正在尝试使用 AlchemyAPI Python 0.7 SDK。但是,当我在其中运行一个方法时,例如 URLGetText(url);

我收到此错误:

    nodes = etree.fromstring(result).xpath(xpathQuery)
  File "lxml.etree.pyx", line 2743, in lxml.etree.fromstring (src/lxml/lxml.etree.c:52665)
  File "parser.pxi", line 1573, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:79932)
  File "parser.pxi", line 1452, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:78774)
  File "parser.pxi", line 960, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:75389)
  File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
  File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
  File "parser.pxi", line 585, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71955)
lxml.etree.XMLSyntaxError: AttValue: " or ' expected, line 19, column 11

这来自这个代码区域:

  def GetRequest(self, apiCall, apiPrefix, paramObject):
    endpoint = 'http://' + self._hostPrefix + '.alchemyapi.com/calls/' + apiPrefix + '/' + apiCall
    endpoint += '?apikey=' + self._apiKey + paramObject.getParameterString()
    handle = urllib.urlopen(endpoint)
    result = handle.read()
    handle.close()
    xpathQuery = '/results/status'
    nodes = etree.fromstring(result).xpath(xpathQuery)
    if nodes[0].text != "OK":
      raise Exception, 'Error making API call.'
    return result

有人对出了什么问题有任何想法吗?

谢谢你

丹尼尔·克肖

4

1 回答 1

0

我查看了 Python urllib 文档,发现了这个页面:

http://docs.python.org/library/urllib.html#high-level-interface

filehandle其中包含有关由以下对象返回的对象的警告urllib.urlopen()

一个警告:read() 方法,如果 size 参数被省略或为负,可能直到数据流结束才读取;在一般情况下,没有很好的方法来确定来自套接字的整个流是否已被读取。

我认为也许您应该确保在使用etree.fromstring()API 解析文件之前将文件的全部内容作为 Python 字符串获取。就像是:

result = ''
while (1):
    next = handle.read()
    if not next: 
        break
    result += next
于 2011-10-15T16:22:03.960 回答