0

Python で Google App Engine を使用しており、GZipped XML ファイルを取得して LXML の iterparse で解析しようとしています。lxml.de の例を使用して、次のコードを作成しました。

import gzip, base64, StringIO
from lxml import etree
from google.appengine.ext import webapp
from google.appengine.api.urlfetch import fetch

class Catalog(webapp.RequestHandler):
user = xxx
password = yyy
catalog = fetch('url',
                    headers={"Authorization": 
                             "Basic %s" % base64.b64encode(user + ':' + password)}, deadline=600)
items = etree.iterparse(StringIO.StringIO(catalog), tag='product')

for _, element in items:
    print('%s -- %s' % (element.findtext('name'), element[1].text))
    element.clear()

実行すると、次のエラーが表示されます。

for _, element in coupons:
File "iterparse.pxi", line 491, in lxml.etree.iterparse.__next__ (src/lxml\lxml.etree.c:98565)
File "iterparse.pxi", line 543, in lxml.etree.iterparse._read_more_events (src/lxml\lxml.etree.c:99086)
File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml\lxml.etree.c:74791)
XMLSyntaxError: Specification mandate value for attribute object, line 1, column 53

このエラーはどういう意味ですか? XML ファイルの形式が正しくないと推測していますが、どこで問題を探すべきかわかりません。どんな助けでも大歓迎です!

4

1 に答える 1

2

この問題は、fetch/gzip 部分を別の方法で処理し、非同期リクエストを有効にし、webapp2 を使用することで解決されました。これをすべて使用するとうまくいきました:)コードは次のとおりです。

from google.appengine.api.urlfetch import fetch
import gzip, webapp2, base64, StringIO, datetime
from credentials import CJCredentials
from lxml import etree

class Catalog(webapp2.RequestHandler):
def get(self):
    user = xxx
    password = yyy
    url = 'some_url'

    catalogResponse = fetch(url, headers={
        "Authorization": "Basic %s" % base64.b64encode(user + ':' + password)
    }, deadline=10000000)

    f = StringIO.StringIO(catalogResponse.content)
    c = gzip.GzipFile(fileobj=f)
    content = c.read()

    xml = StringIO.StringIO(content)

    tree = etree.iterparse(xml, tag='product')

    for event, element in tree:
       print element.name
于 2013-02-15T13:36:12.247 に答える