在谷歌应用程序引擎,如何显示在Python中获取URL的网页的HTML源代码?(In Google

2019-10-29 13:02发布

在谷歌应用程序引擎我发现这个代码是抓取网页的网址:

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

这是fecth该网页的HTML源代码正确的代码? 请问结果变量包含HTML SORCE http://www.google.com/ ? 如果是的话,Python的命令,我应该以显示HTML源代码在这里使用的,而不是doSomethingWithResult(result.content)? 打印结果似乎并没有被正确的方式。

Answer 1:

是的, result.content将包含网页的原始内容。 您应该检查Content-Type头,并验证它要么text/htmlapplication/xhtml+xml

要编写网页的响应的内容,先写你的状态和头,然后:

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


文章来源: In Google App Engines, how to display the HTML source of a page of a fetched URL in Python?