[b68ae8d] | 1 | import unicodedata
|
---|
| 2 | from datetime import datetime
|
---|
| 3 |
|
---|
| 4 | import psycopg2
|
---|
| 5 | import config_read
|
---|
| 6 | from bs4 import BeautifulSoup
|
---|
| 7 | from selenium import webdriver
|
---|
| 8 | import requests
|
---|
| 9 |
|
---|
| 10 | import sys
|
---|
| 11 |
|
---|
| 12 | file_path = 'outputfile.txt'
|
---|
| 13 | sys.stdout = open(file_path, "w")
|
---|
| 14 |
|
---|
| 15 | # Call to read the configuration file and connect to database
|
---|
| 16 | cinfo = config_read.get_databaseconfig("../postgresdb.config")
|
---|
| 17 | db_connection = psycopg2.connect(
|
---|
| 18 | database=cinfo[0],
|
---|
| 19 | host=cinfo[1],
|
---|
| 20 | user=cinfo[2],
|
---|
| 21 | password=cinfo[3]
|
---|
| 22 | )
|
---|
| 23 | cur = db_connection.cursor()
|
---|
| 24 |
|
---|
| 25 | offer_shop = "Ledikom" # offer shop
|
---|
| 26 | last_updated = datetime.now().date()
|
---|
| 27 | is_validated = False
|
---|
| 28 |
|
---|
| 29 | ledikom_phone_urls = [
|
---|
| 30 | 'https://ledikom.mk/c/416/uredi/apple/iphone?limit=96',
|
---|
| 31 | 'https://ledikom.mk/c/421/uredi/samsung/telefoni?limit=96',
|
---|
| 32 | 'https://ledikom.mk/c/424/mobilni-telefoni/xiaomi/telefoni?limit=96',
|
---|
| 33 | 'https://ledikom.mk/c/430/uredi/huawei/telefoni?limit=96',
|
---|
| 34 | 'https://ledikom.mk/c/441/uredi/oneplus/telefoni?limit=96',
|
---|
| 35 | 'https://ledikom.mk/c/413/uredi/google/telefoni?limit=96',
|
---|
| 36 | 'https://ledikom.mk/c/411/uredi/honor/telefoni?limit=96',
|
---|
| 37 | 'https://ledikom.mk/c/460/uredi/nokia/telefoni?limit=96',
|
---|
| 38 | 'https://ledikom.mk/c/461/uredi/asus/telefoni?limit=96',
|
---|
| 39 | 'https://ledikom.mk/c/488/proizvodi/oppo/telefoni?limit=96'
|
---|
| 40 | ]
|
---|
| 41 |
|
---|
| 42 | for ledikom_url in ledikom_phone_urls:
|
---|
| 43 |
|
---|
| 44 | # selenium is used because of the dynamic content of the page
|
---|
| 45 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 46 | driver1.get(ledikom_url)
|
---|
| 47 | ledikom_html = driver1.page_source
|
---|
| 48 |
|
---|
| 49 | # closing the driver so the safari instance can pair with another webdriver session
|
---|
| 50 | driver1.close()
|
---|
| 51 |
|
---|
| 52 | soup1 = BeautifulSoup(ledikom_html, 'html.parser')
|
---|
| 53 |
|
---|
| 54 | phones = soup1.find('div', {'id': 'content'}) \
|
---|
| 55 | .find('div', {'class': 'container'}).find('div', {'class': 'row'}).find('div', {'class': 'item-display'}) \
|
---|
| 56 | .find_all('div', {'class': 'item-in-grid'})
|
---|
| 57 |
|
---|
| 58 | if len(phones) == 0:
|
---|
| 59 | continue
|
---|
| 60 |
|
---|
| 61 | for phone in phones:
|
---|
| 62 | offer_url = 'https://ledikom.mk' + phone.find('a').get('href')
|
---|
| 63 | image_url = phone.find('a').find('img').get('src')
|
---|
| 64 | temp_offer_name = phone.find('div', {'class': 'item-name'}).find('a').get_text().strip()
|
---|
| 65 | offer_name = ' '.join(temp_offer_name.split())
|
---|
| 66 | brand = offer_name.split(' ')[0]
|
---|
| 67 | price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '').replace('.', '').strip())
|
---|
| 68 |
|
---|
| 69 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 70 | driver1.get(offer_url)
|
---|
| 71 | # getting offer page html
|
---|
| 72 | offer_html = driver1.page_source
|
---|
| 73 | driver1.close()
|
---|
| 74 |
|
---|
| 75 | soup2 = BeautifulSoup(offer_html, 'html.parser')
|
---|
| 76 |
|
---|
| 77 | specifications = soup2.find('div', {'id': 'content'}).find('section', {'class': 'padding-section'}) \
|
---|
| 78 | .find_all('div', {'class': 'container'})[1].find('div', {'class': 'col-md-7'}) \
|
---|
| 79 | .find_all('div', {'class': 'row'})
|
---|
| 80 |
|
---|
| 81 | color = None
|
---|
| 82 | rom_memory = None
|
---|
| 83 | ram_memory = None
|
---|
| 84 |
|
---|
| 85 | if len(specifications) != 0:
|
---|
| 86 | colors_tags = specifications[0].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 87 | temp_colors = []
|
---|
| 88 | for color_tag in colors_tags:
|
---|
| 89 | temp_colors.append(color_tag.get_text().strip())
|
---|
| 90 | color = ','.join(temp_colors)
|
---|
| 91 |
|
---|
| 92 | if len(specifications) >= 2:
|
---|
| 93 | temp_rom = specifications[1].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 94 | rom_list = []
|
---|
| 95 | for rom in temp_rom:
|
---|
| 96 | rom_list.append(rom.get('title'))
|
---|
| 97 | rom_memory = ','.join(rom_list)
|
---|
| 98 |
|
---|
| 99 | if len(specifications) >= 3:
|
---|
| 100 | temp_ram = specifications[2].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 101 | ram_list = []
|
---|
| 102 | for ram in temp_ram:
|
---|
| 103 | ram_list.append(ram.get('title'))
|
---|
| 104 |
|
---|
| 105 | ram_memory = ','.join(ram_list)
|
---|
| 106 |
|
---|
| 107 | if 'Xiaomi' in brand:
|
---|
| 108 | temp = color
|
---|
| 109 | color = rom_memory
|
---|
| 110 | rom_memory = temp
|
---|
| 111 |
|
---|
| 112 | temp = ram_memory
|
---|
| 113 | ram_memory = color
|
---|
| 114 | color = temp
|
---|
| 115 |
|
---|
| 116 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url,' \
|
---|
| 117 | 'ram_memory, rom_memory, color, last_updated, is_validated)' \
|
---|
| 118 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
| 119 | insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory,
|
---|
| 120 | rom_memory, color, last_updated, is_validated)
|
---|
| 121 | cur.execute(insert_script, insert_value)
|
---|
| 122 | db_connection.commit()
|
---|
| 123 |
|
---|
| 124 | cur.close()
|
---|
| 125 | db_connection.close()
|
---|