问题描述
Python用正则表达式解析HTML代码时报错:TypeError: expected string or bytes-like object
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210619112306713.png#pic_center)
问题分析
示例代码
bs_xml = BeautifulSoup(a, 'html.parser')
tables = re.findall('(<table>.*?</table>)', bs_xml)
我们在正则匹配符是字符串类型,而我们获取的HTML类型是bytes,所有报错。
解决方案
tables = re.findall('(<table>.*?</table>)', str(bs_xml))
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210619112908396.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzNDUzNzg0,size_16,color_FFFFFF,t_70#pic_center)