1 | from datetime import datetime
|
---|
2 |
|
---|
3 | import psycopg2
|
---|
4 | import config_read
|
---|
5 | from bs4 import BeautifulSoup
|
---|
6 | import requests
|
---|
7 |
|
---|
8 | # import sys
|
---|
9 | # file_path = 'outputfile.txt'
|
---|
10 | # sys.stdout = open(file_path, "w")
|
---|
11 |
|
---|
12 | # Call to read the configuration file and connect to database
|
---|
13 | cinfo = config_read.get_databaseconfig("../postgresdb.config")
|
---|
14 | db_connection = psycopg2.connect(
|
---|
15 | database=cinfo[0],
|
---|
16 | host=cinfo[1],
|
---|
17 | user=cinfo[2],
|
---|
18 | password=cinfo[3]
|
---|
19 | )
|
---|
20 | cur = db_connection.cursor()
|
---|
21 |
|
---|
22 | mobitech_url = "https://mobitech.mk/shop/"
|
---|
23 |
|
---|
24 | response1 = requests.get(mobitech_url)
|
---|
25 |
|
---|
26 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
27 |
|
---|
28 | phones = soup1.find_all('div', {'class': 'jet-woo-products__inner-box'})
|
---|
29 |
|
---|
30 | offer_shop = "Mobitech" # offer shop
|
---|
31 | is_validated = False
|
---|
32 |
|
---|
33 | for phone in phones:
|
---|
34 | offer_url = phone.find('h5', {'class': 'jet-woo-product-title'}).find('a').get('href') # url
|
---|
35 | image_url = phone.find('div', {'class': 'jet-woo-product-thumbnail'}).find('img').get('src') # image
|
---|
36 | brand = phone.find_next('div', {'class': 'jet-woo-product-categories'}).find('a').get_text().strip() # brand
|
---|
37 | offer_name = phone.find('h5', {'class': 'jet-woo-product-title'}).find('a').get_text().strip() # offer_name
|
---|
38 | if brand not in offer_name:
|
---|
39 | offer_name = brand+" "+offer_name
|
---|
40 | temp_prices = phone.find('div', {'class': 'jet-woo-product-price'}).find_all('bdi')
|
---|
41 | price = int(float(temp_prices[len(temp_prices) - 1].get_text().replace("ден", "").replace(",", "").strip())) # price
|
---|
42 | last_updated = datetime.now().date() # offer last_updated date
|
---|
43 |
|
---|
44 | response2 = requests.get(offer_url)
|
---|
45 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
46 |
|
---|
47 | specifications = soup2.find_all('h2', {'class': 'elementor-heading-title elementor-size-default'})
|
---|
48 |
|
---|
49 | ram_memory = ""
|
---|
50 | rom_memory = ""
|
---|
51 | battery = ""
|
---|
52 | back_camera = ""
|
---|
53 | operating_system = ""
|
---|
54 |
|
---|
55 | for specification in specifications:
|
---|
56 | # rom memory
|
---|
57 | if specification.get_text().startswith("Меморија:"):
|
---|
58 | rom_memory = specification.get_text().split("Меморија:")[1].strip()
|
---|
59 | if rom_memory == "Нема" or rom_memory == "/":
|
---|
60 | rom_memory = None
|
---|
61 |
|
---|
62 | # ram memory
|
---|
63 | if specification.get_text().startswith("РАМ Меморија:"):
|
---|
64 | ram_memory = specification.get_text().split("РАМ Меморија:")[1].strip()
|
---|
65 | if ram_memory == "Нема" or ram_memory == "/":
|
---|
66 | ram_memory = None
|
---|
67 |
|
---|
68 | # camera
|
---|
69 | if specification.get_text().startswith("Камера:"):
|
---|
70 | back_camera = specification.get_text().split("Камера:")[1].strip()
|
---|
71 | if back_camera == "Нема":
|
---|
72 | back_camera = None
|
---|
73 |
|
---|
74 | # operating system
|
---|
75 | if specification.get_text().startswith("Оперативен систем:"):
|
---|
76 | operating_system = specification.get_text().split("Оперативен систем:")[1].split(",")[0].strip()
|
---|
77 | if operating_system == "Нема":
|
---|
78 | operating_system = None
|
---|
79 |
|
---|
80 | # battery
|
---|
81 | if specification.get_text().startswith("Батерија:"):
|
---|
82 | battery = specification.get_text().split("Батерија:")[1].strip()
|
---|
83 | if battery == "Нема":
|
---|
84 | battery = None
|
---|
85 |
|
---|
86 | insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory,' \
|
---|
87 | ' rom_memory, battery, back_camera, last_updated, operating_system, is_validated)' \
|
---|
88 | ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
|
---|
89 | insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url, ram_memory,
|
---|
90 | rom_memory, battery, back_camera, last_updated, operating_system, is_validated)
|
---|
91 | cur.execute(insert_script, insert_value)
|
---|
92 | db_connection.commit()
|
---|
93 |
|
---|
94 | cur.close()
|
---|
95 | db_connection.close()
|
---|