十二(1)、Python遇到的问题

1、写爬虫代码,报错:

TypeError: cannot use a string pattern on a bytes-like object

出错的主要原因是因为:
TypeError: can’t use a string pattern on a bytes-like object.
html用decode(‘utf-8’)进行解码,由bytes变成string。
py3的urlopen返回的不是string是bytes。

解决办法:https://blog.youkuaiyun.com/tzs_1041218129/article/details/52228905
把’html’类型调整一下:html.decode(‘utf-8’)

参考:https://blog.youkuaiyun.com/jieli_/article/details/70166244
一劳永逸解决:TypeError: cannot use a string pattern on a bytes-like object

import chardet #需要导入这个模块,检测编码格式
encode_type = chardet.detect(html)
html = html.decode(encode_type[‘encoding’]) #进行相应解码,赋给原标识符(变量)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值