Jul 10, 2024
input()
to ask the user for the product name (e.g., graphics card model).gpu = input("What GPU do you want to search for?")
d=3080&n=4131
for in-stock RTX 3080.requests
to get the page content and read with Beautiful Soup.page = requests.get(url).text
doc = BeautifulSoup(page, 'html.parser')
pages = int(doc.find(class_='list-tool-pagination-text').strong.string.split()[-1])
num_pages
to collect all results.page=x
).for page in range(1, pages+1)
item_container = item.find_parent(class_='item-container')
find
to get specific child elements like price and link.items_found[name] = {'price': price, 'link': link}
price = int(price.replace(',', ''))
import requests
from bs4 import BeautifulSoup
import re
# Get user input
product = input("What product do you want to search for?")
# URL construction
url = f'https://www.newegg.com/p/pl?d={product}&N=4131'
page = requests.get(url).text
# Parsing HTML
doc = BeautifulSoup(page, 'html.parser')
# Determine number of pages
pages = int(doc.find(class_='list-tool-pagination-text').strong.string.split()[-1])
# Initialize results dictionary
items_found = {}
# Loop through pages
for page_num in range(1, pages + 1):
page_url = f'{url}&page={page_num}'
page_content = requests.get(page_url).text
doc = BeautifulSoup(page_content, 'html.parser')
# Find items
items = doc.find_all(text=re.compile(product))
for item in items:
parent = item.find_parent('a', class_='item-title')
link = parent['href'] if parent else None
price = parent.find_next('li', class_='price-current').strong.string
price = int(price.replace(',', ''))
# Store in dictionary
items_found[item] = {'price': price, 'link': link}
# Sort and display results
sorted_items = sorted(items_found.items(), key=lambda x: x[1]['price'])
for item in sorted_items:
print(f'{item[0]}: ${item[1]['price']}, Link: {item[1]['link']}')
BeautifulSoup
to find text.