Changeset 895cd87 for phonelux_scrappers/scrappers/neptun_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/neptun_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 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 16 24 17 offer_shop = "Neptun" # offer shop … … 26 19 is_validated = False 27 20 21 # Neptun phone offers that are already in database 22 23 offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/neptun').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 = [] 39 28 40 for i in range(1, 11): 29 neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page=' +str(i)41 neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page=' + str(i) 30 42 31 43 # selenium is used because of the dynamic content of the page … … 72 84 offer_description = specifications_table.get_text(separator='\n').strip() 73 85 86 back_camera = None 74 87 operating_system = None 75 88 chipset = None … … 78 91 rom_memory = None 79 92 cpu = None 93 front_camera = None 94 color = None 95 80 96 for specification in specifications: 81 97 if 'Батерија:' in specification: … … 105 121 operating_system = specification 106 122 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() 123 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory, 124 color, front_camera, back_camera, chipset, battery, operating_system, cpu, 125 image_url, 126 offer_url, last_updated, is_validated, offer_description, offer_shop_code)) 116 127 117 cur.close() 118 db_connection.close() 128 for new_offer in new_offers: 129 flag = False 130 flag_price = False 131 offer_id = None 132 133 for old_offer in database_offers: 134 135 if new_offer.offer_shop_code == old_offer.offer_shop_code: 136 flag = True 137 if new_offer.price != old_offer.price: 138 flag_price = True 139 offer_id = old_offer.offer_id 140 141 if flag: 142 # print('ALREADY IN DATABASE') 143 # print(new_offer) 144 # if it's already in database, check PRICE and if it's changed, change it !!!!!! 145 if flag_price: 146 print('PRICE CHANGED!') # CHANGE PRICE 147 print('offer id: ' + str(offer_id)) 148 headers = {'Content-type': 'application/json'} 149 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price), 150 headers=headers) 151 else: 152 print('ADDED') # ADD OFFER 153 print(new_offer) 154 headers = {'Content-type': 'application/json'} 155 requests.post('http://localhost:8080/phoneoffer/addoffer', 156 headers=headers, data=json.dumps(new_offer.__dict__, default=str)) 157 158 print('------------------------------------') 159 160 for old_offer in database_offers: 161 flag = False 162 for new_offer in new_offers: 163 if old_offer.offer_shop_code == new_offer.offer_shop_code: 164 flag = True 165 166 if not flag: 167 print('OFFER DELETED') 168 print(old_offer) 169 # DELETE OFFER 170 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note:
See TracChangeset
for help on using the changeset viewer.