当URL和代码中遇到乱码

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壁纸
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值