In Google App Engines, how to display the HTML source of a page of a fetched URL in Python?

流过昼夜 提交于 2019-12-24 19:25:13

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/1868587/in-google-app-engines-how-to-display-the-html-source-of-a-page-of-a-fetched-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!