commit be2a0e947bf59ffc9f784992c42ac1e30df60844 Author: Slava Rogozhkin Date: Tue Jul 15 23:20:03 2025 +0300 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..fdf0fd9 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Парсер цен онлайн маркетов diff --git a/data/bz.sqlite b/data/bz.sqlite new file mode 100644 index 0000000..3cca1ca Binary files /dev/null and b/data/bz.sqlite differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fe7ed94 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,24 @@ +attrs==25.3.0 +certifi==2025.7.14 +charset-normalizer==3.4.2 +configparser==7.2.0 +fake-useragent==2.2.0 +h11==0.16.0 +idna==3.10 +outcome==1.3.0.post0 +PySocks==1.7.1 +requests==2.32.4 +selenium==4.34.2 +selenium-stealth==1.0.6 +setuptools==80.9.0 +sniffio==1.3.1 +sortedcontainers==2.4.0 +SQLite4==0.1.1 +trio==0.30.0 +trio-websocket==0.12.2 +typing_extensions==4.14.1 +undetected-chromedriver==3.5.5 +urllib3==2.5.0 +websocket-client==1.8.0 +websockets==15.0.1 +wsproto==1.2.0 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..1d70ad4 --- /dev/null +++ b/src/main.py @@ -0,0 +1,97 @@ +#TODO: add old_price and new_price to db +#TODO: from db: +# [X] - get product +# [X] - add new product +# [ ] - format price string +# [ ] - delete product +# [ ] - update product +# [ ] - сompare product +# [ ] - recalculate id +#TODO: headless chrome with virtual display + +import sqlite3 +import time +import undetected_chromedriver as uc + +db = "test" +keys = ['id', 'url', 'product', 'old_price', 'new_price'] + +# Markets +dns_elements = ["product-card-top__title", "product-buy__price"] + +def get_db_product(_id: int): + try: + connection = sqlite3.Connection('../data/bz.sqlite') + cursor = connection.cursor() + cursor.execute(f'select * from {db} where id in ({_id})') + except FileNotFoundError: + cursor.close() + else: + product = dict(zip(keys, cursor.fetchone())) + cursor.close() + if 'product' in locals(): + return product + else: + return 1 + + +def add_db_product(_url, _product, _old_price, _new_price): + connection = sqlite3.Connection('../data/bz.sqlite') + cursor = connection.cursor() + cursor.execute(f'insert into {db} (url, name, old_price, new_price) values("{_url}", "{_product}", {_old_price}, {_new_price})') + connection.commit() + cursor.close() + return 0 + +def delete_db_product(): + pass + + +def update_db_product(): + pass + + +def recalculate_db_id(): + pass + + +def compare_product(): + pass + + +# input: _elements = [name = CLASS_NAME, price = CLASS_NAME] +# output: url_product = [ url, product = VALUE[CLASS_NAME], new_price = VALUE[CLASS_NAME]] +def get_url_product_info(_url, _elements: list): + options = uc.ChromeOptions() + #options.headless = True + options.add_argument("--window-size=1920,1080") + options.add_argument('--ignore-certificate-errors') + options.add_argument('--allow-running-insecure-content') + #options.add_argument('--headless') + + driver = uc.Chrome(use_subprocess=True, options=options) + driver.get(_url) + time.sleep(5) + + driver.save_screenshot('nowsecure.png') + + values = [] + for element in _elements: + values.append(driver.find_element(uc.By.CLASS_NAME, element).text) + + _keys = ["product", "price"] + url_product = dict(zip(_keys, values)) + url_product["url"] = _url + return url_product + + +if __name__ == "__main__": + _url = "https://www.dns-shop.ru/product/e2814125168d9c2d/provodnye-nausniki-axxa-aw-02-belyj/" + _product = "headphones" + _old_price = 50 + _new_price = 50 + _id = 3 + + #add_db_product(_url, _product, _old_price, _new_price) + print(get_db_product(5)) +