【发布时间】:2011-07-11 09:00:32
【问题描述】:
我正在尝试从魔兽世界军械库中读取一些 xml(是的,我就是其中之一) - 诸如 this 之类的 url 在 Firefox 中返回 xml(您需要查看源代码才能看到它)但是不在其他浏览器中,例如 Chrome(我不完全理解为什么 - 尽管这是一个旁白)。
无论如何,当我在本地运行应用程序时,我的这段代码运行良好,但现在我正在迁移到 Google App Engine,它不是,我不知道为什么。但似乎无法获取 xml。我已经使用 Beautiful Coup 来解析完整代码中的 xml。
import urllib2,urllib
import socket
from BeautifulSoup import BeautifulStoneSoup
class Object:
def __init__(self):
self.data = {}
self.userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4"
def _getXml(self):
strFile = ""
try:
url = "http://eu.wowarmory.com/guild-info.xml?r=dentarg&n=penance"
values = {}
headers = { 'User-Agent' : self.userAgent }
data = urllib.urlencode(values)
socket.setdefaulttimeout(2)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
strFile = response.read()
except Exception, e:
raise e
finally:
return strFile
def getObject(self):
soup = BeautifulStoneSoup( self._getXml() )
return soup.guildheader["faction"]
这里是主要部分:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from library import Object
class MainHandler(webapp.RequestHandler):
def get(self):
test = Object().getObject()
self.response.out.write(test)
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
我已简化代码以更好地说明问题。如果有任何帮助,我将不胜感激。
【问题讨论】:
-
您的管理控制台的“日志”页面中的某处可能有一条错误消息。请在此处发布。
-
回溯(最近一次调用最后):文件“/Users/colmbrophy/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext /webapp/__init__.py”,第 515 行,在 call 文件中“/Users/colmbrophy/Code/Python/Google App Engine/Test/main.py”,第 47 行,在 get self.response .out.write(soup.guildheader["faction"]) TypeError: 'NoneType' object is unsubscriptable
-
它没有找到任何东西的事实就是为什么我认为它完全无法抓取 xml。
-
当我访问您指定的链接时,我收到重定向,没有 XML 内容。 “这些不是您要查找的军械库页面。”
-
如果您查看源代码,xml 就在那里。
标签: python xml google-app-engine beautifulsoup