【问题标题】:In Google App Engines, how to display the HTML source of a page of a fetched URL in Python?在 Google App Engines 中,如何在 Python 中显示获取的 URL 页面的 HTML 源代码?
【发布时间】:2010-12-24 12:36:42
【问题描述】:

在 Google App Engine 上,我发现这段代码正在获取网页的 URL:

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

这是验证该页面的 HTML 源代码的正确代码吗? result 变量是否包含http://www.google.com/ 的 HTML 源?如果是,我应该在这里使用什么 Python 命令而不是 doSomethingWithResult(result.content) 来显示该 HTML 源代码? 打印结果似乎不是正确的方式。

【问题讨论】:

  • 它对你有用吗?代码对我不起作用!

标签: python html google-app-engine url fetch


【解决方案1】:

是的,result.content 将包含该页面的原始内容。您应该检查Content-Type 标头并确认它是text/htmlapplication/xhtml+xml

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多