Fetching url in python with google app engine

三世轮回 提交于 2019-12-22 01:12:11

问题


I'm using this code to send Http request inside my app and then show the result:

def get(self):
      url = "http://www.google.com/"
      try:
          result = urllib2.urlopen(url)
          self.response.out.write(result)
      except urllib2.URLError, e:

I expect to get the html code of google.com page, but I get this sign ">", what the wrong with that ?


回答1:


You need to call the read() method to read the response. Also good practice to check the HTTP status, and close when your done.

Example:

url = "http://www.google.com/"
try:
    response = urllib2.urlopen(url)

    if response.code == 200:
        html = response.read()
        self.response.out.write(html)
    else:
        # handle

    response.close()

except urllib2.URLError, e:
    pass



回答2:


Try using the urlfetch service instead of urllib2:

Import urlfetch:

from google.appengine.api import urlfetch

And this in your request handler:

def get(self):
    try:
        url = "http://www.google.com/"
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            self.response.out.write(result.content)
        else:
            self.response.out.write("Error: " + str(result.status_code))
    except urlfetch.InvalidURLError:
        self.response.out.write("URL is an empty string or obviously invalid")
    except urlfetch.DownloadError:
        self.response.out.write("Server cannot be contacted")

See this document for more detail.



来源:https://stackoverflow.com/questions/23036285/fetching-url-in-python-with-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!