url = 'http://www.baidu.com'
try:
response = urllib.request.urlopen(url,timeout=3)
except urllib.error.URLError:
print("网络地址错误:")
exit()
with open('g://baidu.txt','w') as fp:
fp.write(response.read().decode('utf-8'))
报错如下
gbk' codec can't encode character '\xbb' in position 27226: illegal multibyte sequence
解决方式保存方式修改
with open('g://baidu.txt','w',encoding='utf-8') as fp:
fp.write(response.read().decode('utf-8'))