[b68ae8d] | 1 | import unicodedata
|
---|
| 2 | from datetime import datetime
|
---|
| 3 |
|
---|
| 4 | import psycopg2
|
---|
| 5 | import config_read
|
---|
| 6 | from bs4 import BeautifulSoup
|
---|
| 7 | import requests
|
---|
| 8 |
|
---|
| 9 | import sys
|
---|
| 10 |
|
---|
| 11 | file_path = 'outputfile.txt'
|
---|
| 12 | sys.stdout = open(file_path, "w")
|
---|
| 13 |
|
---|
| 14 | # Call to read the configuration file and connect to database
|
---|
| 15 | cinfo = config_read.get_databaseconfig("../postgresdb.config")
|
---|
| 16 | db_connection = psycopg2.connect(
|
---|
| 17 | database=cinfo[0],
|
---|
| 18 | host=cinfo[1],
|
---|
| 19 | user=cinfo[2],
|
---|
| 20 | password=cinfo[3]
|
---|
| 21 | )
|
---|
| 22 | cur = db_connection.cursor()
|
---|
| 23 |
|
---|
| 24 | offer_shop = "Setec" # offer shop
|
---|
| 25 | last_updated = datetime.now().date()
|
---|
| 26 | is_validated = False
|
---|
| 27 |
|
---|
| 28 | for i in range(1, 7):
|
---|
| 29 | setec_url = 'https://setec.mk/index.php?route=product/category&path=10066_10067&page='+str(i)
|
---|
| 30 |
|
---|
| 31 | response1 = requests.get(setec_url)
|
---|
| 32 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
| 33 |
|
---|
| 34 | phones = soup1.find('div', {'id': 'mfilter-content-container'}) \
|
---|
| 35 | .find_all('div', {'class': 'col-sm-4 col-xs-6'})
|
---|
| 36 |
|
---|
| 37 | for phone in phones:
|
---|
| 38 | offer_url = phone.find('div', {'class': 'left'}).find('a').get('href')
|
---|
| 39 | image_url = phone.find('div', {'class': 'left'}).find('a').find('img').get('src')
|
---|
| 40 | offer_name = phone.find('div', {'class': 'right'}).find('div', {'class': 'name'}).find('a').get_text().strip()
|
---|
| 41 | brand = offer_name.split(' ')[0]
|
---|
| 42 |
|
---|
| 43 | if 'Cable' in offer_name or 'AirTag' in offer_name:
|
---|
| 44 | continue
|
---|
| 45 |
|
---|
| 46 | if brand not in offer_name:
|
---|
| 47 | offer_name = brand + " " + offer_name
|
---|
| 48 |
|
---|
| 49 | offer_shop_code = phone.find('div', {'class': 'right'}) \
|
---|
| 50 | .find('div', {'class': 'shifra'}).get_text().replace('Шифра:', '').strip()
|
---|
| 51 | price = int(phone.find('div', {'class': 'right'}).find('div', {'class': 'price'}). \
|
---|
| 52 | find('div', {'class': 'category-price-redovna'}).find('span', {'class': 'price-old-new'}) \
|
---|
| 53 | .get_text().replace('Ден.', '').replace(',', '').strip())
|
---|
| 54 |
|
---|
| 55 | response2 = requests.get(offer_url)
|
---|
| 56 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
| 57 |
|
---|
| 58 | offer_description = soup2.find('div', {'id': 'tab-description'}).get_text(separator='\n')
|
---|
| 59 |
|
---|
| 60 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, image_url, offer_url,' \
|
---|
| 61 | 'offer_shop_code, offer_description, last_updated, is_validated)' \
|
---|
| 62 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
| 63 | insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url,
|
---|
| 64 | offer_shop_code, offer_description, last_updated, is_validated)
|
---|
| 65 | cur.execute(insert_script, insert_value)
|
---|
| 66 | db_connection.commit()
|
---|
| 67 |
|
---|
| 68 | cur.close()
|
---|
| 69 | db_connection.close()
|
---|