Advertisement
skip420

webfart

Aug 15th, 2021
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. from urllib.request import urlopen as uReq
  2. from bs4 import BeautifulSoup as soup
  3.  
  4. #builds the URL
  5. middleURL = input('Search for a product on newegg: ').replace(" ", "+")
  6. frontURL = 'https://www.newegg.com/Product/ProductList.aspx?Submit=ENE&DEPA=0&Order=BESTMATCH&Description='
  7. backURL = '&N=-1&isNodeId=1'
  8. url = frontURL + middleURL + backURL
  9.  
  10. #grabs the page
  11. page = uReq(url)
  12. page_html = page.read()
  13. page.close()
  14.  
  15. #html parsing
  16. page_soup = soup(page_html, "html.parser")
  17.  
  18. #grabs all product containers
  19. containers = page_soup.findAll("div", {"class":"item-container"})
  20.  
  21. #makes a csv file
  22. filename = "products.csv"
  23. f = open(filename, "w")
  24. headers = "Product_Name, Price, Shipping_Cost\n"
  25. f.write(headers)
  26.  
  27. #search starts at index 4, 0-3 are ads
  28. for i in range(4, len(containers)):
  29.     container = containers[i]
  30.  
  31.     #grabs item title
  32.     titleContainer = container.findAll("a", {"class":"item-title"})
  33.     productName = titleContainer[0].text.strip()
  34.  
  35.     #grabs item price, might need try except
  36.     priceContainer = container.findAll("li", {"class":"price-current"})
  37.     productPrice = priceContainer[0].text.strip()
  38.     #grabs only the item price from productPrice, discards the useless text
  39.     productPriceList = productPrice.split()
  40.     for x in range(len(productPriceList)):
  41.         if "$" in productPriceList[x]:
  42.             productPrice = productPriceList[x]
  43.             break
  44.  
  45.     #grabs shipping price
  46.     shippingContainer = container.findAll("li", {"class":"price-ship"})
  47.     shippingCost = shippingContainer[0].text.strip()
  48.  
  49.     print(productName)
  50.     print(productPrice)
  51.     print(shippingCost)
  52.  
  53.     #writes in the csv file, calls replace to get rid of commas
  54.     f.write(productName.replace(",", "|") + "," + productPrice.replace(",", "") + "," + shippingCost.replace(",", "") + "\n")
  55.  
  56. f.close()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement