urllib2没有正确获取XML

时间:2013-03-22 00:29:30

标签: python xml google-app-engine

我在Google App Engine中有一个python应用程序,当我尝试获取XML Feed时,没有任何反应,除了在AppEngineLauncher的日志控制台中,我得到了

WARNING  2013-03-21 23:37:39,482 urlfetch_stub.py:453] Stripped prohibited headers from URLFetch request: ['Host']

这是我的代码:

class FindStops(webapp2.RequestHandler):
    def post(self):
        bound=self.request.POST["bound"]
        line=self.request.POST["stop"]
        stops=urllib2.urlopen("http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=" + line)
        tree=ET.parse(stops)
        root=tree.getroot()
        title=list()
        for info in tree.findall('stop'):
            title.append(info.get('title'))

        for name in title:
            self.response.out.write(name)

这就是XML的样子:

<body copyright="All data copyright San Francisco Muni 2013.">
<route tag="F" title="F-Market & Wharves" color="555555" oppositeColor="ffffff"    latMin="37.7625199" latMax="37.8085899" lonMin="-122.43487" lonMax="-122.39345">
<stop tag="5184" title="Jones St & Beach St" lat="37.8072499" lon="-122.41737" stopId="15184"/>
<stop tag="3092" title="Beach St & Mason St" lat="37.80741" lon="-122.4141199" stopId="13092"/>
<stop tag="3095" title="Beach St & Stockton St" lat="37.8078399" lon="-122.41081" stopId="13095"/>
.
.
.

我很确定我正确地解析了XML,但self.response.out.write什么都没打印出来,所以我在这里不知所措。

1 个答案:

答案 0 :(得分:1)

我肯定会建议添加:

logging.info( 'stops: ' + stops.read(100) )

在使用urllib2.urlopen()后立即查看您是否在指责urllib2之前得到了正确的答案。