【问题标题】:Print HTML From Url [duplicate]从 URL 打印 HTML [重复]
【发布时间】:2016-10-03 09:07:54
【问题描述】:

所以我想打印出一个网站的HTML

from urllib.request import urlopen

http = urlopen('http://www.google.de/').read()
print(http)

但在输出中,所有换行符都打印为\n,并且字符串以 b' 开头,这与我的谷歌研究告诉我的咬数组有关?抱歉,我是 python xD 的新手

所以我的问题是如何将 html 代码打印为带有换行符的普通字符串,因为它会在文本编辑器中显示?

【问题讨论】:

    标签: python python-3.x urllib urlopen


    【解决方案1】:

    查看urlopen 文档。在 HTML 标头中写为charset=UTF-8。因此,您需要将您的线路更改为:

    print(http.decode('utf-8'))
    

    如果您在 HTML 输出中有特殊字符(由于区域设置),请使用:

    print(http.decode('utf-8', errors='ignore'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2023-03-28
      • 2018-12-31
      • 1970-01-01
      • 2016-01-10
      • 2016-07-16
      相关资源
      最近更新 更多