def aptekaru_parser_to_db(): for url in URLs.query.filter(URLs.store_id == 6).all(): try: response = urllib.request.urlopen(url.drug_url, context=ctx).read() soup = BeautifulSoup(response, "html.parser") try: try: prices_all = soup.find( attrs={"class": "price m--mobile_font "}) price = prices_all.span.text except: prices_all = soup.find( attrs={"class": "price new_price m--mobile_font "}) price = prices_all.span.text else: pass except: price = "0.00" if len(price) > 0: price = price else: price = "0.00" except TimeoutError: price = '0.00' except urllib.error.URLError: price = '0.00' except urllib.error.HTTPError: price = '0.00' drug = Drugs.query.filter(Drugs.id == url.drug_id).first() store = Stores.query.filter(Stores.id == url.store_id).first() to_db = Prices(date, drug, store, price) db_session.add(to_db) #time.sleep(15) time.sleep(random.randint(21, 38)) db_session.commit()
def samsonpharma_parser_to_db(): for url in URLs.query.filter(URLs.store_id == 1).all(): try: response = urllib.request.urlopen(url.drug_url, context=ctx).read() soup = BeautifulSoup(response, "html.parser") try: prices_all = soup.findAll( attrs={"class": "product__price-value"}) price = prices_all[0].text price = re.findall('(\d+)', price) price = price[0] + '.00' except: price = "0.00" if len(price) > 0: price = price else: price = "0.00" except TimeoutError: price = '0.00' except urllib.error.URLError: price = '0.00' except urllib.error.HTTPError: price = '0.00' drug = Drugs.query.filter(Drugs.id == url.drug_id).first() store = Stores.query.filter(Stores.id == url.store_id).first() to_db = Prices(date, drug, store, price) db_session.add(to_db) time.sleep(15) db_session.commit()
def rigla_parser_to_db(): for url in URLs.query.filter(URLs.store_id == 7).all(): try: response = urllib.request.urlopen(url.drug_url, context=ctx).read() soup = BeautifulSoup(response, "html.parser") try: prices_all = soup.find(attrs={"itemprop": "price"}) price = prices_all.text price = price.replace(',', '.') except: price = "0.00" if len(price) > 0: price = price else: price = "0.00" except TimeoutError: price = '0.00' except urllib.error.URLError: price = '0.00' except urllib.error.HTTPError: price = '0.00' drug = Drugs.query.filter(Drugs.id == url.drug_id).first() store = Stores.query.filter(Stores.id == url.store_id).first() to_db = Prices(date, drug, store, price) db_session.add(to_db) time.sleep(15) db_session.commit()