python - 类型错误 : unhashable type: 'list' for webscraping project

标签 python dictionary hash

我正在制作一个抓取该网站的程序。收集数据,它只是项目的名称,我可以使用它们的平台,以及它们的价格。我为每一个被抓取的信息制作了一个数据结构。但是我在创建字典时提示类型错误?

我正在使用 python 3.7.2。在 Windows 10 上运行。

import requests
import bs4
import time
from bs4 import BeautifulSoup as Bsoup

url = "https://ebgames.com.au/search?q=Skyrim"
resp = requests.get(url)
soup = Bsoup(resp.text, 'html.parser')
platforms = soup.select(".product-top-level-group")
price = soup.select(".price")
names = soup.select(".product-title")
stripped_names = [na.text.strip() for na in names]
stripped_prices = [pri.text.strip() for pri in price]
stripped_platforms = [plat.text.strip() for plat in  platforms]




Game = {
    (stripped_names): {
        "Price": (stripped_prices),
        "Platform": [stripped_platforms]


    }
}

for Gamename, Gameinfo in Game.items():
    print(Gamename)
    print("Platform:", Gameinfo['Platform'])
    print("Price:", Gameinfo['Price'])
    print("\n")

这是我的错误:

"Platform": [stripped_platforms]
TypeError: unhashable type: 'list'

最佳答案

不确定你从哪里得到 dict 初始化语法,但这不是它在 Python 中的完成方式。

这里有一个使用 zip 的好方法:

stripped_names = ['Skyrim', 'Minecraft']
stripped_prices = ['$59.99', '$19.99']
stripped_platforms = ['PC', 'XBox One']

Game = {
    name: {
        "Price": price,
        "Platform": platform,
    } for name, price, platform in zip(
        stripped_names,
        stripped_prices,
        stripped_platforms,
    )
}

for Gamename, Gameinfo in Game.items():
    print(Gamename)
    print("Platform:", Gameinfo['Platform'])
    print("Price:", Gameinfo['Price'])
    print("\n")

输出:

Skyrim
Platform: PC
Price: $59.99


Minecraft
Platform: XBox One
Price: $19.99

关于python - 类型错误 : unhashable type: 'list' for webscraping project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990891/

相关文章:

C++ 将 map<int, set<int>> 转换为 map<int, vector<int>>

c# - IComparer 可以用于在填充列表时对列表进行哈希吗?

c++ - 计算无符号字符的长度

python - 有没有办法用 python 检查列表中的字符串是否是普通英语中使用的真实单词?

python - npm install grunt-takana 在 node-gyp 重建时失败

python - 在 python 中编写提醒/秒表程序的最佳方法?

Python——从字符串中生成字典

javascript - 在 HTML 模板中使用 Javascript 检索 Flask JSON 对象

python - 为什么给字典的键 `True` 赋值会覆盖同一字典中键 `1` 的值?

c# - SHA512 哈希到 C# 中的字符串