source: phonelux_scrappers/scrappers/neptun_scrapper.py@ dfd5d87

Last change on this file since dfd5d87 was b68ae8d, checked in by Marko <Marko@…>, 23 months ago

Created spring app, edited setec_scrapper

  • Property mode set to 100644
File size: 4.8 KB
Line 
1import unicodedata
2from datetime import datetime
3import psycopg2
4import config_read
5from bs4 import BeautifulSoup
6from selenium import webdriver
7import requests
8
9import sys
10
11file_path = 'outputfile.txt'
12sys.stdout = open(file_path, "w")
13
14# Call to read the configuration file and connect to database
15cinfo = config_read.get_databaseconfig("../postgresdb.config")
16db_connection = psycopg2.connect(
17 database=cinfo[0],
18 host=cinfo[1],
19 user=cinfo[2],
20 password=cinfo[3]
21)
22cur = db_connection.cursor()
23
24offer_shop = "Neptun" # offer shop
25last_updated = datetime.now().date()
26is_validated = False
27
28for 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
117cur.close()
118db_connection.close()
Note: See TracBrowser for help on using the repository browser.