Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'

1、TypeError: expected string or bytes-like object

  1. 问题:使用BeautifulSoup解析网页,使用正则提取,提示报错

Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'

  1. 分析提示信息:expected string or bytes-like object
    第62行传入值的类型应该为string或者object

  2. 打印传入的值:title的类型

Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'

  1. OK,现在转换值的类型

Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'
运行成功。

2、TypeError: unhashable type: ‘list’

  1. 问题:把两个列表组合成一个字典,提示:TypeError: unhashable type: ‘list’

Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'

  1. 分析,提示信息解释为:list是不可哈希的

可以哈希的类型:不可变的数据结构(字符串str、元组tuple、对象集objects)。

不可哈希的类型:可变的数据结构 (字典dict,列表list,集合set)

  1. 解决方法:通过索引生成一个个小字典(zip),然后将一个个字典update到一个大字典中。
    Python爬虫出现:TypeError: expected string or bytes-like object 和 TypeError: unhashable type: 'list'
    OK,解决。