Advertisement
DeaD_EyE

ifm.com get products

Jan 14th, 2025 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. import json
  2. from pathlib import Path
  3. from urllib.parse import urljoin
  4.  
  5. import bs4
  6. import requests
  7.  
  8. BASE_URL = "https://ifm.com"
  9. REST_URL = "https://www.ifm.com/restservices/{}/productsAndAttributes"
  10.  
  11. def get_categories():
  12.     return {
  13.         e.text: e["href"]
  14.         for e in bs4.BeautifulSoup(
  15.             requests.get(urljoin(BASE_URL, "/de/de/category")).content, "html.parser"
  16.         ).select("ul.sub-categories--secondary li a")
  17.     }
  18.  
  19.  
  20. def get_sub_categories(link):
  21.     return {
  22.         e.text.strip(): e["href"]
  23.         for e in bs4.BeautifulSoup(
  24.             requests.get(urljoin(BASE_URL, link)).content,
  25.             "html.parser",
  26.         ).select("a.tile__link-wrapper")
  27.     }
  28.  
  29.  
  30. product_file = Path.home().joinpath("Desktop", "ifm", "product_pages.json")
  31. product_file.parent.mkdir(exist_ok=True)
  32.  
  33. if not product_file.exists():
  34.     product_pages = []
  35.     for description, link in get_categories().items():
  36.         print(description)
  37.         print("=" * len(description))
  38.         sub_categories = get_sub_categories(link)
  39.         if sub_categories:
  40.             for description, link in sub_categories.items():
  41.                 print(description)
  42.                 product_pages.append(link)
  43.             print("\n\n")
  44.         else:
  45.             print("\n")
  46.             product_pages.append(link)
  47.     with product_file.open("w") as fd:
  48.         product_pages = json.dump(product_pages, fd)
  49. else:
  50.     with product_file.open("r") as fd:
  51.         product_pages = json.load(fd)
  52.  
  53.  
  54. for products in product_pages:
  55.     url = REST_URL.format(products)
  56.     for product in requests.get(url).json().get("productResults", []):
  57.         attributes = product.get("attributes", {})
  58.         name = attributes.get("at_produktbezeichnung", "NoName")
  59.         price = attributes.get("formattedPrice", "NoPrice")
  60.         if product_id := attributes.get("productId"):
  61.             print(name, price)
  62.             with product_file.parent.joinpath(f"{product_id}.json").open("w") as fd:
  63.                 json.dump(attributes, fd)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement