Skip to content

Instantly share code, notes, and snippets.

@daniyalb
Created February 22, 2023 23:11
Show Gist options
  • Save daniyalb/42106c74afe20f2c442b796dba727d28 to your computer and use it in GitHub Desktop.
Save daniyalb/42106c74afe20f2c442b796dba727d28 to your computer and use it in GitHub Desktop.
Demo code from Web Scraping Workshop
from bs4 import BeautifulSoup
import requests
product = input('What are you searching for?: ')
product = product.replace(' ', '+')
sort = input('Lowest price (ls), Best Selling (bs), Best Rated (br): ')
if sort == 'ls':
sort = 1
elif sort == 'bs':
sort = 3
else:
sort = 4
url = f'https://www.newegg.ca/p/pl?d={product}&Order={sort}'
html = requests.get(url).text
soup = BeautifulSoup(html, 'lxml')
products = soup.find('div', class_="item-cells-wrap border-cells items-grid-view four-cells expulsion-one-cell")
target = products.div
title_tag_a = target.find('a', class_='item-title')
title = title_tag_a.text
link = title_tag_a['href']
price_tag = target.find('li', class_='price-current')
price_dollar = price_tag.strong.text
price_cents = price_tag.sup.text
print()
print('Heres what your looking for:')
print(f'name: {title}')
print(f'price: ${price_dollar}{price_cents}')
print(f'link: {link}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment