Python3基础 response.read 输出网页的源代码

188 阅读1分钟
  •        Python : 3.8.11
  •          OS : Ubuntu Kylin 20.04
  •       Conda : 4.10.1
  •    jupyter lab : 3.1.4

代码

import urllib.request



url = ""

# 服务器给的响应
response = urllib.request.urlopen(url)

# 返回一个二进制字符串: b'',无法正常阅读
html = response.read()

# 进行解码操作
code_of_html = html.decode('utf-8')

# 打印查看网页源代码
print(code_of_html)

学习推荐


Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。