[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 = "Mobelix" # offer shop
|
---|
| 25 | last_updated = datetime.now().date()
|
---|
| 26 | is_validated = False
|
---|
| 27 |
|
---|
| 28 | for i in range(1, 17):
|
---|
| 29 | mobelix_url = "https://mobelix.com.mk/mk/mobilni-telefoni?page=" + str(i)
|
---|
| 30 |
|
---|
| 31 | response1 = requests.get(mobelix_url)
|
---|
| 32 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
| 33 |
|
---|
| 34 | phones = soup1.find_all('div', {'class': 'p-2 rounded text-dark bg-white d-flex w-100'})
|
---|
| 35 |
|
---|
| 36 | for phone in phones:
|
---|
| 37 | offer_url = phone.find('a').get('href')
|
---|
| 38 | image_url = phone.find_all('div', {'class': 'col-12'})[0].find('img').get('src')
|
---|
| 39 | brand = phone.find_all('div', {'class': 'col-12'})[1].find('h5', {'class': 'mb-0'}).get_text().strip()
|
---|
| 40 | offer_name = phone.find_all('div', {'class': 'col-12'})[1] \
|
---|
| 41 | .find('h3', {'class': 'h5 font-weight-normal'}).get_text().strip()
|
---|
| 42 |
|
---|
| 43 | if 'Watch' in offer_name or 'Pad' in offer_name or 'Tab' in offer_name or 'Pods' in offer_name or 'Buds' in offer_name or 'HomePod' in offer_name:
|
---|
| 44 | continue
|
---|
| 45 |
|
---|
| 46 | if brand not in offer_name:
|
---|
| 47 | offer_name = brand + " " + offer_name
|
---|
| 48 |
|
---|
| 49 | temp_prices = phone.find_all('div', {'class': 'col-12'})[1] \
|
---|
| 50 | .find('p', {'class': 'h5 price'}).get_text(separator='/').strip()
|
---|
| 51 |
|
---|
| 52 | if len(temp_prices.split('/')) > 1:
|
---|
| 53 | price = int(float(temp_prices.split('/')[1].replace(',', '').replace('ден', '').strip()))
|
---|
| 54 | else:
|
---|
| 55 | price = int(float(temp_prices.split('/')[0].replace(',', '').replace('ден', '').strip()))
|
---|
| 56 |
|
---|
| 57 | response2 = requests.get(offer_url)
|
---|
| 58 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
| 59 |
|
---|
| 60 | colors_divs = soup2.find('div', {'class': 'color-wrapper mt-2 mb-1'}) \
|
---|
| 61 | .find_all('div', {'class': 'color-box d-inline-block'}) # color div tags
|
---|
| 62 |
|
---|
| 63 | temp_colors = []
|
---|
| 64 | for div in colors_divs:
|
---|
| 65 | temp_colors.append(div.get('title'))
|
---|
| 66 |
|
---|
| 67 | color = ",".join(temp_colors) # available colors for offer
|
---|
| 68 |
|
---|
| 69 | tables = soup2.find('div', {'class': 'mobelix-specs table-white bordered-table'}).find_all('table')
|
---|
| 70 |
|
---|
| 71 | operating_system = None
|
---|
| 72 | chipset = None
|
---|
| 73 | battery = None
|
---|
| 74 | ram_memory = None
|
---|
| 75 | rom_memory = None
|
---|
| 76 | front_camera = ''
|
---|
| 77 | back_camera = ''
|
---|
| 78 | cpu = None
|
---|
| 79 |
|
---|
| 80 | for table in tables:
|
---|
| 81 | for cell in table.find_all('td'):
|
---|
| 82 | if cell.get('data-spec') is None:
|
---|
| 83 | continue
|
---|
| 84 |
|
---|
| 85 | if cell.get('data-spec') == 'os':
|
---|
| 86 | operating_system = unicodedata.normalize('NFKD', cell.get_text().strip())
|
---|
| 87 |
|
---|
| 88 | if cell.get('data-spec') == 'chipset':
|
---|
| 89 | chipset = unicodedata.normalize('NFKD', cell.get_text().strip())
|
---|
| 90 |
|
---|
| 91 | if cell.get('data-spec') == 'cpu':
|
---|
| 92 | cpu = unicodedata.normalize('NFKD', cell.get_text().strip())
|
---|
| 93 |
|
---|
| 94 | if cell.get('data-spec') == 'internalmemory':
|
---|
| 95 | temp_rom = []
|
---|
| 96 | temp_ram = []
|
---|
| 97 | temp_internalmemory = unicodedata.normalize('NFKD', cell.get_text().strip())
|
---|
| 98 | for internalmemory in temp_internalmemory.split(','):
|
---|
| 99 | temp_rom.append(internalmemory.strip().split(' ')[0])
|
---|
| 100 | if len(internalmemory.strip().split(' ')) > 1:
|
---|
| 101 | temp_ram.append(internalmemory.strip().split(' ')[1])
|
---|
| 102 | rom_memory = ','.join(temp_rom)
|
---|
| 103 | ram_memory = ','.join(temp_ram)
|
---|
| 104 |
|
---|
| 105 | if cell.get('data-spec') == 'cam1modules' or cell.get('data-spec') == 'cam1features' or cell.get(
|
---|
| 106 | 'data-spec') == 'cam1video':
|
---|
| 107 | back_camera += unicodedata.normalize('NFKD', cell.get_text().strip()) + '\n'
|
---|
| 108 |
|
---|
| 109 | if cell.get('data-spec') == 'cam2modules' or cell.get('data-spec') == 'cam2features' or cell.get(
|
---|
| 110 | 'data-spec') == 'cam2video':
|
---|
| 111 | front_camera += unicodedata.normalize('NFKD', cell.get_text().strip()) + '\n'
|
---|
| 112 |
|
---|
| 113 | if cell.get('data-spec') == 'batdescription1':
|
---|
| 114 | battery = unicodedata.normalize('NFKD', cell.get_text().strip())
|
---|
| 115 |
|
---|
| 116 | if front_camera == 'No':
|
---|
| 117 | front_camera = None
|
---|
| 118 |
|
---|
| 119 | if back_camera == 'No':
|
---|
| 120 | back_camera = None
|
---|
| 121 |
|
---|
| 122 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url,' \
|
---|
| 123 | 'ram_memory, rom_memory, battery, back_camera, front_camera, color, cpu, chipset, ' \
|
---|
| 124 | 'operating_system, last_updated, is_validated)' \
|
---|
| 125 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
| 126 | insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory, rom_memory,
|
---|
| 127 | battery, back_camera, front_camera, color, cpu, chipset, operating_system,
|
---|
| 128 | last_updated, is_validated)
|
---|
| 129 | cur.execute(insert_script, insert_value)
|
---|
| 130 | db_connection.commit()
|
---|
| 131 |
|
---|
| 132 | cur.close()
|
---|
| 133 | db_connection.close()
|
---|