0

I have this code:

data = []
with open('PRODUCTS.json', 'w', encoding='utf-8') as file_object:
    for link in links:
        
        browser.get(link)

        name = browser.find_element_by_xpath("/html/body/div[7]/div/div[3]/div[1]/div/div/div/div[1]/div[2]/h1").text
        
        try:
            in_stock = browser.find_element_by_xpath("//*[@id='siteContainer']/div[7]/div/div[3]/div[1]/div/div/div/div[2]/div[2]/div[7]/div[2]/div/div[1]/div/div[1]/div/b").text
        except NoSuchElementException:
            in_stock = 'STOCK UNKOWN'

        try:
            price = browser.find_element_by_class_name("site-currency-attention").text
        except NoSuchElementException:
            price = 'PRICE UNKOWN'


        data = {
            (uniqueId): {
                (name): 
                {
                        'link': (browser.current_url),
                        'in-stock': (in_stock),
                        'price': (price),
                        'last_updated': (d1)
                    }
            }
        }
        print("PRODUCT ADDED!")


        json.dump(data, file_object, indent=4)

And I want to add an ID for every entry.

Right now it prints:

{
    "Product name": {
        "link": "link here",
        "in-stock": "12",
        "price": "9.400",
        "last_updated": "03/06/2021 - 10:27:16"
    }
}

I would also, if possible, like the possibility to categorize the JSON file by the product name. The products name have special "model numbers" inside of them example: asdww 3008, asdww 3009, asdww 3010. Is it possible to sort the JSON file by their names?

1 Answers1

0

There are Universally Unique Identifiers available in the standard library module uuid, for example :

>>> import uuid
>>> uuid.uuid4()
UUID('90b5e22d-30bb-4d7d-9133-e38661f5ba17')

Spoiler : you will not have the same than me

Lenormju
  • 4,078
  • 2
  • 8
  • 22