1

I'm making a program that scrapes this website. The data is collected, its only the names of items, platform I can use them on, and their price. I've made a data structure for the each scraped bit of information. But I am prompted with a type error when creating my dictionary?

I'm using python 3.7.2. Operating on 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")

This is my error:

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

2 Answers 2

2

Not sure where you got that dict initialization syntax from, but that's not how it's done in Python.

Here's a nice way of doing it with 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")

Output:

Skyrim
Platform: PC
Price: $59.99


Minecraft
Platform: XBox One
Price: $19.99
1
  • Thanks, Although it works and I'm happy. I don't quite understand how it works. Could you please describe exactly what its doing. Thanks May 5, 2019 at 10:38
1

The problem is that you are trying to initialize a dict with a key that's non-hashable, here stripped_names (you can read this to learn more about hashable objects). You can turn it into a tuple to make it hashable, or chose another key.

Next time, don't forget to post the whole traceback of your error, it will help people help you faster and better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.