-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarrefour.py
62 lines (50 loc) · 2.25 KB
/
Carrefour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import requests
from bs4 import BeautifulSoup
def get_product_info(query, exact):
url = f"https://www.carrefour.ro/catalogsearch/result/?q={query}"
# Send a GET request to the URL and retrieve the HTML content
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find all product products
products = soup.find_all('li', class_='product')
# Extract product information (name and price) for each product
product_info = []
for product in products:
# Extract product id
product_id = product['data-product-id']
# Extract product name
name_element = product.find('div', class_='productItem-name')
name = name_element.find('a').text.strip() if name_element else 'N/A'
product_url=product.find('a', class_='productItem-image').get('href')
# Find the corresponding price element
price_element = soup.find('div', id=f"product-price-{product_id}")
price_amount = price_element['data-price-amount'] if price_element else 'N/A'
# Append product information to the list
if exact:
if query.lower() in name.lower().split():
product_info.append({'name': name, 'price': price_amount,'product_url':product_url})
else:
product_info.append({'name': name, 'price': price_amount,'product_url':product_url})
return product_info
else:
# Handle failed HTTP request
return None
if __name__ == "__main__":
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("Usage: python_script.py <query> [exact]")
sys.exit(1)
query = sys.argv[1]
exact = False
if len(sys.argv) == 3:
exact = True
products = get_product_info(query, exact)
if products:
for product in products:
print(f"{product['name']}\n{product['price']}\n{product['product_url']}")
print("-" * 50)
# else:
# print("No products found.")