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 = "Handy" # offer shop
|
---|
25 | last_updated = datetime.now().date()
|
---|
26 | is_validated = False
|
---|
27 |
|
---|
28 | handy_url = 'https://www.handy.mk/telefoni?page=6'
|
---|
29 |
|
---|
30 | response1 = requests.get(handy_url)
|
---|
31 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
32 |
|
---|
33 | phones = soup1.find_all('li', {'data-hook': 'product-list-grid-item'})
|
---|
34 |
|
---|
35 | for phone in phones:
|
---|
36 | offer_url = phone.find('a').get('href')
|
---|
37 | offer_name = phone.find('div', {'data-hook': 'not-image-container'})\
|
---|
38 | .find('h3', {'data-hook': 'product-item-name'}).get_text().strip()
|
---|
39 | brand = offer_name.split(' ')[0].capitalize()
|
---|
40 | price = int(float(phone.find('div', {'data-hook': 'not-image-container'}).find('div', {'data-hook': "product-item-product-details"})\
|
---|
41 | .find('span', {'data-hook': 'product-item-price-to-pay'}).get_text().strip().replace('ден', '').replace('.', '').replace(',', '.')))
|
---|
42 |
|
---|
43 | response2 = requests.get(offer_url)
|
---|
44 | soup2 = BeautifulSoup(response2.text, 'html.parser')
|
---|
45 |
|
---|
46 | color_section = soup2.find('section', {'data-hook': 'product-colors-title-section'})
|
---|
47 |
|
---|
48 | color = None
|
---|
49 | if color_section is not None:
|
---|
50 | temp_colors = color_section.find('fieldset', {'class': 'ColorPickerbase3563640754__container'})\
|
---|
51 | .find_all('input', {'type': 'radio'})
|
---|
52 | colors_list = []
|
---|
53 | for temp_color in temp_colors:
|
---|
54 | colors_list.append(temp_color.get('aria-label'))
|
---|
55 | color = ','.join(colors_list)
|
---|
56 |
|
---|
57 | rows = soup2.find('div', {'data-hook': 'info-section-description'}).find_all('li')
|
---|
58 |
|
---|
59 | if len(rows) == 0:
|
---|
60 | rows = soup2.find('div', {'data-hook': 'info-section-description'}).find_all('tr')
|
---|
61 |
|
---|
62 | specifications = []
|
---|
63 |
|
---|
64 | for row in rows:
|
---|
65 | specifications.append(unicodedata.normalize('NFKD', row.get_text().strip()))
|
---|
66 |
|
---|
67 | offer_description = '\n'.join(specifications)
|
---|
68 |
|
---|
69 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, offer_url, ' \
|
---|
70 | 'offer_description, last_updated, is_validated)' \
|
---|
71 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
72 | insert_value = (offer_shop, brand, offer_name, price, offer_url, offer_description,
|
---|
73 | last_updated, is_validated)
|
---|
74 | cur.execute(insert_script, insert_value)
|
---|
75 | db_connection.commit()
|
---|
76 |
|
---|
77 | cur.close()
|
---|
78 | db_connection.close() |
---|