Engine在python中获取网址

Engine在python中获取网址

本文介绍了使用Google App Engine在python中获取网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在我的应用程序内发送Http请求,然后显示结果:

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:

我希望获得google.com页面的html代码,但是我得到了这个符号>",这有什么问题呢?

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

推荐答案

您需要调用read()方法来读取响应.同样好的做法是检查HTTP状态,并在完成后关闭.

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

示例:

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

这篇关于使用Google App Engine在python中获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:00