我对 Python 和 GAE 非常陌生,但我正在尝试从 eventful.com api(以 XML 格式)下载 XML 文件,对其进行解析,然后我会将这些信息存储在 Google Cloud SQL 上的数据库中。
到目前为止,我的代码如下,我在查看了各种在线教程后设法编写了这些代码,但是我不断收到很多错误,并且代码对我根本不起作用。如果有人对我哪里出错有任何指示,请告诉我,凯伦。
我尝试调用多事的 xml 文件并对其进行解析:
import webapp2
from google.appengine.ext.webapp import template
import os
import datetime
from google.appengine.ext import db
from google.appengine.api import urlfetch
import urllib #import python library which does http requests
from xml.dom import parseString #imports xml parser called minidom
class XMLParser(webapp2.RequestHandler):
def get(self):
base_url = fetch('http://api.eventful.com/rest/events/search?app_key=zGtDX6cwQ=dublin&?q=music')
#downloads data from xml file
response = urllib.urlopen(base_url)
#converts data to string:
data = response.read()
#closes file
response.close()
#parses xml downloaded
dom = parseString(data)
#retrieves the first xml tag that the parser finds with name tag
xmlTag = dom.getElementsByTagName('title')[0].toxml()
#strip off the tag to just reveal event name
xmlData = xmlTag.replace('<title>', '').replace('</title>', '')
#print out the xml tag and data in this format:
print xmlTag
#just print the data
print xmlData
当我尝试运行此代码时收到以下错误,但是在 Google App Engine 用户 GAE 启动器中 -
2013-04-15 16:52:05 Running command: "['C:\\Python27\\python.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8002', u'C:\\Users\\Karen\\Desktop\\Development\\own_tada']"
INFO 2013-04-15 16:52:17,944 devappserver2.py:498] Skipping SDK update check.
WARNING 2013-04-15 16:52:18,005 api_server.py:328] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2013-04-15 16:52:18,065 api_server.py:152] Starting API server at: http://localhost:54619
INFO 2013-04-15 16:52:18,085 dispatcher.py:150] Starting server "default" running at: http://localhost:8080
INFO 2013-04-15 16:52:18,095 admin_server.py:117] Starting admin server at: http://localhost:8002
ERROR 2013-04-15 15:52:35,767 wsgi.py:219]
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 196, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 255, in _LoadHandler
handler = __import__(path[0])
File "C:\Users\Karen\Desktop\Development\own_tada\own.py", line 8, in <module>
from xml.dom import parseString #imports xml parser called minidom
ImportError: cannot import name parseString
INFO 2013-04-15 16:52:35,822 server.py:561] default: "GET / HTTP/1.1" 500 -
ERROR 2013-04-15 15:52:37,586 wsgi.py:219]
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 196, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 255, in _LoadHandler
handler = __import__(path[0])
File "C:\Users\Karen\Desktop\Development\own_tada\own.py", line 8, in <module>
from xml.dom import parseString #imports xml parser called minidom
ImportError: cannot import name parseString
INFO 2013-04-15 16:52:37,617 server.py:561] default: "GET /favicon.ico HTTP/1.1" 500 -
我用于上述代码的一个此类教程来自以下 URL:http ://www.travisglines.com/web-coding/python-xml-parser-tutorial
编辑:
感谢下面 Josh 提供的帮助,当我使用我的代码启动我的代码时,我现在没有收到任何错误,但是我只看到一个空白屏幕并希望它打印出解析的信息(或到目前为止的进度)。我知道这似乎是一个非常愚蠢的问题,但我真的是一个初学者,所以我很抱歉!固定代码(减去错误)是:
import webapp2
from google.appengine.ext.webapp import template
import os
import datetime
from google.appengine.ext import db
from google.appengine.api import urlfetch
import urllib #import python library which does http requests
import xml.dom.minidom as mdom #imports xml parser called minidom
class XMLParser(webapp2.RequestHandler):
def get(self):
base_url = 'http://api.eventful.com/rest/events/search?app_key=zGtDX6cwQjCRdkf6&l=dublin&?q=music'
#downloads data from xml file
response = urllib.urlopen(base_url)
#converts data to string:
data = response.read()
#closes file
response.close()
#parses xml downloaded
dom = mdom.parseString(data)
#retrieves the first xml tag that the parser finds with name tag
xmlTag = dom.getElementsByTagName('title')[0].toxml()
#strip off the tag to just reveal event name
xmlData = xmlTag.replace('<title>', '').replace('</title>', '')
#print out the xml tag and data in this format:
print xmlTag
#just print the data
print xmlData
app = webapp2.WSGIApplication([('/', XMLParser),
],
debug=True)
任何关于下一步做什么的指导将不胜感激,或者任何你能发现的我的 python 代码有问题,谢谢!