VsKraper / demo.py
usserrr's picture
Upload folder using huggingface_hub
740b369
Raw History Blame Contribute Delete
3.14 kB
import random
import requests
from bs4 import BeautifulSoup
with open('user-agents.txt', 'r') as file:
user_agents = file.read().splitlines()
# Randomly select a user agent
selected_user_agent_index = random.randint(0, len(user_agents) - 1)
selected_user_agent = user_agents[selected_user_agent_index]
print(f"User Agent Number: {selected_user_agent_index}")
headers = {
'User-Agent': selected_user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'TE': 'Trailers',
}
url = input("Enter URL of the Flipkart product: ")
base_url = 'https://flipkart.com'
# Send a GET request to the Flipkart product page with the specified headers
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
print("Success!")
else:
print("Failed to retrieve the product page.")
exit()
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Extract product information with error handling
title_element = soup.find('span', {'class': 'B_NuCI'})
title = title_element.get_text().strip() if title_element else "Title not found"
price_element = soup.find('div', {'class': '_30jeq3'})
price = price_element.get_text().strip() if price_element else "Price not found"
rating_element = soup.find('div', {'class': '_3LWZlK'})
rating = rating_element.text if rating_element else "Rating not found"
num_reviews_element = soup.find('span', {'class': '_2_R_DZ'})
num_reviews = num_reviews_element.text if num_reviews_element else "Number of Reviews not found"
description_element = soup.find('div', {'class': '_1mXcCf'})
description = description_element.get_text().strip() if description_element else "Description not found"
# Find image links
image_links = []
image_elements = soup.find_all('img', {'class': '_396cs4'})
for img in image_elements:
if 'src' in img.attrs:
image_links.append(img['src'])
if response.status_code == 200:
# Parse the HTML content of the page
soup = BeautifulSoup(response.content, "html.parser")
# Find the elements with class "_3UAT2v _16PBlm"
elements = soup.find_all(class_="_3UAT2v _16PBlm")[:3] # Fetching the first 3 elements
for element in elements:
# Find the parent 'a' element and get its href attribute
parent_a = element.find_parent('a')
if parent_a and 'href' in parent_a.attrs:
href = parent_a['href']
review_url = base_url+href
else:
print("Failed to fetch the page. Status code:", response.status_code)
# Print the extracted information
print("Product Title:", title)
print("\nImage Links:")
for index, link in enumerate(image_links, start=1):
print(f"Image {index}: {link}")
print("Product Price:", price)
print("Product Rating:", rating)
print("Number of Reviews:", num_reviews)
print("Product Description:", description)
print("Review_url:", review_url)