取页面源代码

点击查看代码

#打开一个网址
from urllib.request import urlopen
url="http://www.baidu.com"

res=urlopen(url)

# print(res.read().decode("utf-8"))#拿到的是网页源代码
#
with open("mybaidu.htm",mode="w",encoding="utf-8")as f:
f.write(res.read().decode("utf-8"))