Python将网页源代码写入文件出错 'gbk' codec can't encode character '\xef'
Super
2019-11-30 10:21
3635
def get_uri_text(): url = m3u8_url.get('0.0', 'end') headers = { 'Content-Type': 'text/html;charset=utf-8', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' } t = requests.get(url, stream=True, headers=headers) f = open("test_writer.txt", "w+") f.write(t.text) f.close() # 写入文件出错 # UnicodeEncodeError: 'gbk' codec can't encode character '\xef' in position 2456: illegal multibyte sequence # 解决方法 def get_uri_text(): url = m3u8_url.get('0.0', 'end') headers = { 'Content-Type': 'text/html;charset=utf-8', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' } t = re.get(url, stream=True, headers=headers) f = open("test_writer.txt", "w+", encoding='utf-8') #以utf-8编码写入文件 t.encoding = 'utf-8' f.write(t.text) f.close()
0 条讨论