:[Errno 13]在appengine上使用cElementTree的权限被拒绝
问题描述:
尝试从互联网源获取xml文件并解析其中的某些值。 我的代码:<type'exceptions.IOError'>:[Errno 13]在appengine上使用cElementTree的权限被拒绝
def get_xml(self):
#xml_url = "http://www.shoppingcar.it/feed/export_vel.asp?parametro=1"
xml_url = "http://dl.dropbox.com/u/102727/AutoVeloce%20XML%20Splitter/shoppingcar.xml"
MAX_RETRY = 3
retry_left = MAX_RETRY
while retry_left:
try:
req = urlfetch.fetch(xml_url, deadline=10)
XML = req.content.decode('windows-1252')
break # <- done! got file.
except (urllib2.HTTPError, DownloadError):
retry_left -= 1
logging.error("Error while downloading ShoppingCar.xml, retrying.")
except DeadlineExceededError:
retry_left -= 1
logging.warning("DeadlineExceededError while downloading ShoppingCar.xml, retrying.")
except Timeout:
retry_left -= 1
logging.warning("Timeout while downloading ShoppingCar.xml, retrying.")
if retry_left and retry_left < MAX_RETRY:
logging.info("Downloading ShoppingCar.xml succeeded after %s retries.", MAX_RETRY - retry_left)
if not retry_left:
logging.error("Downloading ShoppingCar.xml failed after %s retries.", MAX_RETRY)
return ET.parse(XML)
def _iter_carDicts_in_xml(self, newOnly=True):
#fails here >
tree = self.get_xml()
for veicolo in tree.getiterator('veicolo'):
# do stuff
错误消息我得到:
<type 'exceptions.IOError'>: [Errno 13] Permission denied: u'<?xml version="1.0" encoding="windows-1252"?><veicoli>\r\n <veicolo>\r\n\t\t<id><![CDATA[26806]]></id>
这个错误继续吐出的xml文件,所以我不会粘贴所有
真的不知道去哪里这一个,我用类似的代码从XML文件中提取,从来没有这种问题。有任何想法吗?
答
parse
函数需要文件对象。改为尝试parse(StringIO(XML))
。
这确实让我过去了IOError,谢谢。但是,现在我有':找不到任何元素:行1,列0',但我会认为这是一个单独的错误,并将其标记为正确。再次感谢。 –
Milo
我添加的代码如下所示:'import StringIO','XMLio = StringIO.StringIO(XML)','return ET.parse(XMLio)' – Milo
好吧,我想了一切。你通常应该可以执行'ET.parse(urllib.urlopen('url'))',问题在于GAE的'urlfetch'没有返回ET寻找的东西(我不知道细节)。我所知道的是'ET.parse(urllib.urlopen('url'))'工作得很好,我可以用这行下载和解析xml文件。 – Milo