在Google App Engines中,如何在Python中显示已获取URL的页面的HTML源代码?

bri*_*ant 0 html python url google-app-engine fetch

在Google App Engine上,我发现此代码正在获取网页的网址:

from google.appengine.api import urlfetch
url = "http://www.google.com/"
result = urlfetch.fetch(url)
if result.status_code == 200:
  doSomethingWithResult(result.content)
Run Code Online (Sandbox Code Playgroud)

这是用于确定该页面的HTML源代码的正确代码吗?结果变量是否包含http://www.google.com/的 HTML文件?如果是,我应该使用什么Python命令而不是 doSomethingWithResult(result.content)才能显示该HTML源代码?打印结果似乎不是正确的方法.

Bob*_*man 5

是的,result.content将包含该页面的原始内容.你应该检查Content-Type标题并确认它是text/html或者application/xhtml+xml.

要将该页面的内容写入响应,请首先编写您的状态和标题,然后:

self.response.out.write(result.content)
Run Code Online (Sandbox Code Playgroud)