[b68ae8d] | 1 | import unicodedata
|
---|
| 2 | from datetime import datetime
|
---|
| 3 | import psycopg2
|
---|
| 4 | import config_read
|
---|
| 5 | from bs4 import BeautifulSoup
|
---|
| 6 | from selenium import webdriver
|
---|
| 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 = "Neptun" # offer shop
|
---|
| 25 | last_updated = datetime.now().date()
|
---|
| 26 | is_validated = False
|
---|
| 27 |
|
---|
| 28 | for i in range(1, 11):
|
---|
| 29 | neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page='+str(i)
|
---|
| 30 |
|
---|
| 31 | # selenium is used because of the dynamic content of the page
|
---|
| 32 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 33 | driver1.get(neptun_url)
|
---|
| 34 | neptun_html = driver1.page_source
|
---|
| 35 |
|
---|
| 36 | # closing the driver so the safari instance can pair with another webdriver session
|
---|
| 37 | driver1.close()
|
---|
| 38 |
|
---|
| 39 | # response1 = requests.get(neptun_url)
|
---|
| 40 | soup1 = BeautifulSoup(neptun_html, 'html.parser')
|
---|
| 41 |
|
---|
| 42 | phones = soup1.find('div', {'id': 'mainContainer'}).find('div',
|
---|
| 43 | {'class': 'col-lg-9 col-md-9 col-sm-8 col-fix-main'}) \
|
---|
| 44 | .find_all('div', {'class': 'ng-scope product-list-item-grid'})
|
---|
| 45 |
|
---|
| 46 | for phone in phones:
|
---|
| 47 | offer_url = 'https://www.neptun.mk' + phone.find('a').get('href')
|
---|
| 48 | offer_name = phone.find('a').find('h2').get_text().replace('MOB.TEL.', '').strip()
|
---|
| 49 | brand = offer_name.split(' ')[0].strip().capitalize()
|
---|
| 50 | image_url = 'https://www.neptun.mk' + phone.find('a').find('div', {'class': 'row'}).find('img').get('src')
|
---|
| 51 | price = int(
|
---|
| 52 | phone.find('div', {'class': 'col-sm-12 static'}).find('div', {'class': 'product-list-item__prices pt35'})
|
---|
| 53 | .find('div', {'class': 'row'}).find('div', {'class': 'newPriceModel'}) \
|
---|
| 54 | .find('span', {'class': 'product-price__amount--value ng-binding'}).get_text().replace('.', ''))
|
---|
| 55 |
|
---|
| 56 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 57 | driver1.get(offer_url)
|
---|
| 58 | offer_html = driver1.page_source
|
---|
| 59 | # closing the driver so the safari instance can pair with another webdriver session
|
---|
| 60 | driver1.close()
|
---|
| 61 |
|
---|
| 62 | soup2 = BeautifulSoup(offer_html, 'html.parser')
|
---|
| 63 |
|
---|
| 64 | offer_shop_code = soup2.find('div', {'ng-if': 'showProductDetails'}) \
|
---|
| 65 | .find('div', {'class': 'product-details-first-row'}).find('span', {
|
---|
| 66 | 'ng-bind': 'model.CodeNumber'}).get_text().strip()
|
---|
| 67 |
|
---|
| 68 | specifications_table = \
|
---|
| 69 | soup2.find('div', {'id': 'mainContainer'}).find('div', {'ng-if': 'showProductDetails'}).find_all('ul')[-1]
|
---|
| 70 | specifications = specifications_table.get_text(separator='\n').strip().split("\n")
|
---|
| 71 |
|
---|
| 72 | offer_description = specifications_table.get_text(separator='\n').strip()
|
---|
| 73 |
|
---|
| 74 | operating_system = None
|
---|
| 75 | chipset = None
|
---|
| 76 | battery = None
|
---|
| 77 | ram_memory = None
|
---|
| 78 | rom_memory = None
|
---|
| 79 | cpu = None
|
---|
| 80 | for specification in specifications:
|
---|
| 81 | if 'Батерија:' in specification:
|
---|
| 82 | battery = specification.split('Батерија:')[1]
|
---|
| 83 |
|
---|
| 84 | if 'CPU:' in specification:
|
---|
| 85 | cpu = specification.split('CPU:')[1]
|
---|
| 86 |
|
---|
| 87 | if 'Chipset:' in specification:
|
---|
| 88 | chipset = specification.split('Chipset:')[1]
|
---|
| 89 |
|
---|
| 90 | if 'RAM Меморија:' in specification:
|
---|
| 91 | ram_memory = specification.split('RAM Меморија:')[1]
|
---|
| 92 | continue
|
---|
| 93 |
|
---|
| 94 | if 'ROM Меморија:' in specification:
|
---|
| 95 | rom_memory = specification.split('ROM Меморија:')[1]
|
---|
| 96 | continue
|
---|
| 97 |
|
---|
| 98 | if 'ROM:' in specification:
|
---|
| 99 | rom_memory = specification.split('ROM:')[1]
|
---|
| 100 |
|
---|
| 101 | if 'RAM:' in specification:
|
---|
| 102 | ram_memory = specification.split('RAM:')[1]
|
---|
| 103 |
|
---|
| 104 | if 'iOS' in specification or 'Android' in specification:
|
---|
| 105 | operating_system = specification
|
---|
| 106 |
|
---|
| 107 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, image_url, offer_url,' \
|
---|
| 108 | 'offer_shop_code, operating_system, battery, chipset, cpu, ram_memory, rom_memory, ' \
|
---|
| 109 | 'offer_description, last_updated, is_validated)' \
|
---|
| 110 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
| 111 | insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url,
|
---|
| 112 | offer_shop_code, operating_system, battery, chipset, cpu, ram_memory, rom_memory, offer_description,
|
---|
| 113 | last_updated, is_validated)
|
---|
| 114 | cur.execute(insert_script, insert_value)
|
---|
| 115 | db_connection.commit()
|
---|
| 116 |
|
---|
| 117 | cur.close()
|
---|
| 118 | db_connection.close()
|
---|