Changeset 895cd87 for phonelux_scrappers/scrappers/handy_scrapper.py
- Timestamp:
- 10/01/22 22:55:27 (2 years ago)
- Branches:
- master
- Children:
- fd5b100
- Parents:
- 48f3030
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux_scrappers/scrappers/handy_scrapper.py
r48f3030 r895cd87 1 import json 1 2 import unicodedata 2 3 from datetime import datetime … … 9 10 import sys 10 11 12 from classes.phoneoffer import PhoneOffer 13 11 14 file_path = 'outputfile.txt' 12 15 sys.stdout = open(file_path, "w") 13 16 14 # Call to read the configuration file and connect to database15 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 17 24 18 offer_shop = "Handy" # offer shop 25 19 last_updated = datetime.now().date() 26 20 is_validated = False 21 22 # Handy phone offers that are already in database 23 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/handy').text)) 24 25 database_offers = [] 26 27 for offer in offers: 28 phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'], 29 offer['ram_memory'], 30 offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'], 31 offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'], 32 offer['image_url'], 33 offer['offer_url'], offer['last_updated'], offer['is_validated'], 34 offer['offer_description'], 35 offer['offer_shop_code']) 36 database_offers.append(phoneOffer) 37 38 new_offers = [] 27 39 28 40 handy_url = 'https://www.handy.mk/telefoni?page=6' … … 44 56 soup2 = BeautifulSoup(response2.text, 'html.parser') 45 57 58 back_camera = None 59 operating_system = None 60 chipset = None 61 battery = None 62 ram_memory = None 63 rom_memory = None 64 cpu = None 65 front_camera = None 66 offer_shop_code = None 67 color = None 68 image_url = None 69 46 70 color_section = soup2.find('section', {'data-hook': 'product-colors-title-section'}) 47 48 color = None49 71 if color_section is not None: 50 temp_colors = color_section.find('fieldset', {'class': 'ColorPickerbase35 63640754__container'})\72 temp_colors = color_section.find('fieldset', {'class': 'ColorPickerbase3548966286__container'})\ 51 73 .find_all('input', {'type': 'radio'}) 52 74 colors_list = [] … … 67 89 offer_description = '\n'.join(specifications) 68 90 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() 91 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 92 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 93 image_url, 94 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 76 95 77 cur.close() 78 db_connection.close() 96 for new_offer in new_offers: 97 flag = False 98 flag_price = False 99 offer_id = None 100 101 for old_offer in database_offers: 102 103 if new_offer.offer_name == old_offer.offer_name: 104 flag = True 105 if new_offer.price != old_offer.price: 106 flag_price = True 107 offer_id = old_offer.offer_id 108 109 if flag: 110 # print('ALREADY IN DATABASE') 111 # print(new_offer) 112 # if it's already in database, check PRICE and if it's changed, change it !!!!!! 113 if flag_price: 114 print('PRICE CHANGED!') # CHANGE PRICE 115 print('offer id: ' + str(offer_id)) 116 headers = {'Content-type': 'application/json'} 117 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price), 118 headers=headers) 119 else: 120 print('ADDED') # ADD OFFER 121 print(new_offer) 122 headers = {'Content-type': 'application/json'} 123 requests.post('http://localhost:8080/phoneoffer/addoffer', 124 headers=headers, data=json.dumps(new_offer.__dict__, default=str)) 125 126 print('------------------------------------') 127 128 for old_offer in database_offers: 129 flag = False 130 for new_offer in new_offers: 131 if old_offer.offer_name == new_offer.offer_name: 132 flag = True 133 134 if not flag: 135 print('OFFER DELETED') 136 print(old_offer) 137 # DELETE OFFER 138 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id)) 139 140
Note:
See TracChangeset
for help on using the changeset viewer.