yasoob / practical-python-projects

99 stars 43 forks source link

Suggestion for Scraping Steam Project #33

Open graciedevine opened 3 years ago

graciedevine commented 3 years ago

Description

Hello! I was doing the web scraping project in PyCharm and it suggested using a dictionary literal for Lines 27-33. So, instead of:

    resp = {}
    resp['title'] = info[0]
    resp['price'] = info[1]
    resp['tags'] = info[2]
    resp['platforms'] = info[3]
    output.append(resp)

Possible Solutions

PyCharm suggested writing it as:

    resp = {'title': info[0], 'price': info[1], 'tags': info[2], 'platforms': info[3]}
    output.append(resp)

I think this would be good to include since list comprehensions are presented in this chapter too.

Your full name so I can provide accurate credit within the book

Gracie Devine

ivangeorgiev commented 2 years ago

I have a bit different suggestion about this:

from collections import namedtuple

Game = namedtuple("Game", "title,price,tags,platforms")

output = [Game(*info)._asdict() for info in zip(titles, prices, tags, platforms)]

Another approach would be to use custom JSON serializer. Illustrated on dataclass instead of namedtuple:

import json
from dataclasses import dataclass

@dataclass(frozen=True)
class Game:
    title:str
    price:str
    tags:list
    platforms:list

class EnhancedJSONEncoder(json.JSONEncoder):
        def default(self, o):
            if dataclasses.is_dataclass(o):
                return dataclasses.asdict(o)
            return super().default(o)

output = [Game(*info) for info in zip(titles, prices, tags, platforms)]
json.dumps(output, cls=EnhancedJSONEncoder)
feexie commented 10 months ago

cant understand the div class; i try to find the popular release but i couldn't find it! any help or suggestion please?

feexie commented 7 months ago

Thanks

On Tue, Feb 27, 2024 at 4:38 PM Rauan Omirkul @.***> wrote:

cant understand the div class; i try to find the popular release but i couldn't find it! any help or suggestion please?

https://store.steampowered.com/explore/new here is the link. New releases aren't on the homepage

— Reply to this email directly, view it on GitHub https://github.com/yasoob/practical-python-projects/issues/33#issuecomment-1966839800, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4VR6NK735HA5UJOQMMQZGDYVX4WNAVCNFSM423QOGC2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJWGY4DGOJYGAYA . You are receiving this because you commented.Message ID: @.***>