source: phonelux_scrappers/scrappers/setec_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: 2.6 KB
Line 
1import unicodedata
2from datetime import datetime
3
4import psycopg2
5import config_read
6from bs4 import BeautifulSoup
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 = "Setec" # offer shop
25last_updated = datetime.now().date()
26is_validated = False
27
28for i in range(1, 7):
29 setec_url = 'https://setec.mk/index.php?route=product/category&path=10066_10067&page='+str(i)
30
31 response1 = requests.get(setec_url)
32 soup1 = BeautifulSoup(response1.content, 'html.parser')
33
34 phones = soup1.find('div', {'id': 'mfilter-content-container'}) \
35 .find_all('div', {'class': 'col-sm-4 col-xs-6'})
36
37 for phone in phones:
38 offer_url = phone.find('div', {'class': 'left'}).find('a').get('href')
39 image_url = phone.find('div', {'class': 'left'}).find('a').find('img').get('src')
40 offer_name = phone.find('div', {'class': 'right'}).find('div', {'class': 'name'}).find('a').get_text().strip()
41 brand = offer_name.split(' ')[0]
42
43 if 'Cable' in offer_name or 'AirTag' in offer_name:
44 continue
45
46 if brand not in offer_name:
47 offer_name = brand + " " + offer_name
48
49 offer_shop_code = phone.find('div', {'class': 'right'}) \
50 .find('div', {'class': 'shifra'}).get_text().replace('Шифра:', '').strip()
51 price = int(phone.find('div', {'class': 'right'}).find('div', {'class': 'price'}). \
52 find('div', {'class': 'category-price-redovna'}).find('span', {'class': 'price-old-new'}) \
53 .get_text().replace('Ден.', '').replace(',', '').strip())
54
55 response2 = requests.get(offer_url)
56 soup2 = BeautifulSoup(response2.content, 'html.parser')
57
58 offer_description = soup2.find('div', {'id': 'tab-description'}).get_text(separator='\n')
59
60 insert_script = 'INSERT INTO phone_offers (offer_shop, brand, offer_name , price, image_url, offer_url,' \
61 'offer_shop_code, offer_description, last_updated, is_validated)' \
62 ' VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
63 insert_value = (offer_shop, brand, offer_name, price, image_url, offer_url,
64 offer_shop_code, offer_description, last_updated, is_validated)
65 cur.execute(insert_script, insert_value)
66 db_connection.commit()
67
68cur.close()
69db_connection.close()
Note: See TracBrowser for help on using the repository browser.