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