req = request.Request('https://frnks.top') req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36')
mycontext = ssl.SSLContext() #创建context with request.urlopen(req,context=mycontext) as f: # 下文与本文无关 data = f.read() print('Status:', f.status,f.reason) for k, v in f.getheaders(): print(f'{k}:{v}') decoded = data.decode('utf-8') withopen('/Users/frnks/test.html','w',encoding='utf-8') as f: #这里open时就要加上encoding 不然默认以系统编码gbk写入 f.write(decoded) print('Data:', decoded)