diff --git a/IKEA_scrapper/main.py b/IKEA_scrapper/main.py index ddaa14d3..bf3b9de7 100644 --- a/IKEA_scrapper/main.py +++ b/IKEA_scrapper/main.py @@ -4,43 +4,40 @@ import requests prices = [] names = [] -HEADERS = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 OPR/77.0.4054.275'} +HEADERS = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Vivaldi/4.1.2369.21'} class IKEA: - def __init__(self, url, price, name): + def __init__(self, url, name, price): self.url = url - self.price = price self.name = name + self.price = price - def function(self): + def get_data(self): prices.clear() url = self.url page = requests.get(url, headers=HEADERS) soup = BeautifulSoup(page.content, 'html.parser') - for el in soup.find_all(class_=self.price): - cropped_price = el.get_text().strip() # strip necessary info - prices.append(cropped_price) - for el in soup.find_all(class_=self.name): cropped_name = el.get_text().strip() names.append(cropped_name) - # converted_price = list(map(convertor, prices)) # converts . to , - # adds € at the end of each element - # new_prices = map((lambda x: x + "€"), converted_price) - # converts list to string - # output = f"{self.name}\n" + "\n".join(str(elem) for elem in new_prices) - output = names + for el in soup.find_all(class_=self.price): + cropped_price = el.get_text().strip() + prices.append(cropped_price[:cropped_price.find("€") + 1]) + + combined_list = [i + " - " + j for i, j in zip(names, prices)] + output = "\n".join(str(elem) for elem in combined_list) + return output -aizskari = IKEA('https://www.ikea.lv/lv/products/virtuve/aizkari-un-zaluzijas/aizkari', 'itemBTI display-6', 'display-7 mr-2') +curtains = IKEA('https://www.ikea.lv/lv/products/virtuve/aizkari-un-zaluzijas/aizkari', 'display-7 mr-2', 'display-6') def main(): - print(aizskari.function()) + print(curtains.get_data()) if __name__ == '__main__':