html = response.text
# 对文本进行重新编码
html = html.encode('ISO-8859-1')
# print(html)
html = html.decode('utf-8') #解码
# print(html)
1.quote()
该方法可以将内容转化为URL编码格式,URL中带有中文参数时,有时可能会导致乱码的问题,此时用这个方法可以讲中文字符转化为URL编码。
from urllib.parse import quote
keys = '壁纸'
url = 'http://www.baidu.com/s?wd' + quote(keys)
print(url)
# 这里声明了一个中文的搜索文字,然后用quote()方法对其进行URL编码,最后返回结果
http://www.baidu.com/s?wd%E5%A3%81%E7%BA%B8
2.unquote()
有了quote()方法,当然还有unquote()方法,它可以进行URL解码
from urllib.parse import unquote
url = 'http://www.baidu.com/s?wd%E5%A3%81%E7%BA%B8'
print(unquote(url))
# 返回结果
http://www.baidu.com/s?wd壁纸
2387

被折叠的 条评论
为什么被折叠?



