TypeError:不可损坏的类型:webscraping proj的“list”

2024-10-16 22:35:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我使用的是python3.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")

这是我的错误:

^{pr2}$

Tags: textinimportfornamesselectpricestrip
2条回答

不确定您从哪里得到的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")

输出:

^{pr2}$

问题是您试图用一个不可散列的键初始化dict,这里是stripped_names(您可以阅读this来了解有关散列对象的更多信息)。您可以将其转换为tuple以使其可散列,或者选择另一个键。在

下一次,别忘了把你的错误全部回溯出来,它会帮助人们更快更好地帮助你。在

相关问题 更多 >