在Google App Engines中,如何在Python中显示获取的URL的页面的HTML源代码? [英] In Google App Engines, how to display the HTML source of a page of a fetched URL in Python?

查看:131
本文介绍了在Google App Engines中,如何在Python中显示获取的URL的页面的HTML源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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)

这是正确的代码来检查页面的HTML源代码吗? 结果变量是否包含 http://www.google.com/?如果是,那么我应该使用什么Python命令来代替 doSomethingWithResult(result.content)来显示该HTML源代码? c> result.content 将包含该页面的原始内容。您应该检查 Content-Type 标题并确认它是 text / html 应用程序/ xhtml + xml



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

  self.response.out.write(result.content)


On Google App Engine I found this code that is fetching a web page's URL:

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

Is this the right code to fecth that page's HTML source? Does the result variable contain HTML sorce of http://www.google.com/? If yes, what Python command I should use here instead of doSomethingWithResult(result.content) in order to display that HTML source? print result doesn't seem to be the right way.

解决方案

Yes, result.content will contain the raw content of that page. You should check the Content-Type header and verify that it's either text/html or application/xhtml+xml.

To write the content of that page to the response, first write your status and headers and then:

self.response.out.write(result.content)

这篇关于在Google App Engines中,如何在Python中显示获取的URL的页面的HTML源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆