From 498afc699559b1eca6854aae9cf83c59b0da495a Mon Sep 17 00:00:00 2001 From: Peisong Xiao Date: Fri, 23 Feb 2024 23:28:38 -0500 Subject: [PATCH] First commit of the project! Added upc.py - getting the name of an item (in Canada) using its UPC --- .gitignore | 1 + upc.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 .gitignore create mode 100644 upc.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dbd351 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +downloaded_files/ \ No newline at end of file diff --git a/upc.py b/upc.py new file mode 100644 index 0000000..6159ee4 --- /dev/null +++ b/upc.py @@ -0,0 +1,52 @@ +import time +from seleniumbase import Driver + +driver = Driver(uc=True) +driver.implicitly_wait(5) + +def get_name_from_upc(upc: str) -> str: + url = "https://stocktrack.ca/wm/index.php?s=wm&upc=" + upc + driver.get(url) + driver.add_cookie({ + "name": "PHPSESSID", + "value": "835ttgvbgncf4uq82dpagmk688", + "path": "/", + "domain": "stocktrack.ca" + }) + driver.add_cookie({ + "name": "cf_chl_3", + "value": "1fad2e046cb9148", + "path": "/", + "domain": "stocktrack.ca" + }) + driver.add_cookie({ + "name": "cf_clearance", + "value": "LUZoWRwn3n1wBEv7bY1XinLQbX0ZTAnggLIfkoy2S60-1708745781-1.0-AXqZtJSIQrq/6/H03NLlHwbpLA1tJmPjgjMJhzaczZjGabhR6ow16wMK5pTwilZiqK7C7fdcYvM0qsbAARkLGEU=", + "path": "/", + "domain": "stocktrack.ca" + }) + driver.refresh() + name = "" + str_s = "target=\"_blank\">" + str_t = "
UPC: " + upc + "
" + times = 0 + while True: + if __debug__: + print("Iteration No. ", times) + times = times + 1 + + time.sleep(20) + page = str(driver.execute_script( + "return document.getElementsByTagName('html')[0].innerHTML")) + t = page.find(str_t) + s = page.rfind(str_s, 0, t) + + if __debug__: + print(page[s + len(str_s) : t - 1]) + + if t == -1 or s == -1: + continue + else: + name = page[s + len(str_s) : t - 1] + break + return name