-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebScrapper.py
More file actions
55 lines (39 loc) · 1.61 KB
/
webScrapper.py
File metadata and controls
55 lines (39 loc) · 1.61 KB
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
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
# the url of the website we are going to be scrapping
my_url = 'https://www.newegg.com/p/pl?d=graphics+cards'
# Opening up connection, grabbing the page then closing the connection
uClient = uReq(my_url)
page_html = uClient.read()
# Does my html parsing
page_soup = soup(page_html, 'html.parser')
# Grabs each product
containers = page_soup.findAll('div',{'class':'item-container'})
# writes to a file you can change txt to csv for excel
filename = 'products.txt'
f = open(filename, 'w')
headers = 'product_name, shipping\n'
f.write(headers)
# for loop to check if the tags are in the html
for container in containers:
# finds brand in scrapper
#brand = container.div.div.a.img['title']
# finds the product name or title as you can see on the website
title_container = container.findAll('a', {'class':'item-title'})
product_name = title_container[0].text
# finds the shipping price in the html
shipping_container = container.findAll('li', {'class': 'price-ship'})
if shipping_container == "Free Shipping":
shipping = "Free Shipping"
else:
shipping = "Not free shipping"
# prints the product name and shipping with price of shipping
#print('brand: ' + brand)
print('product: ' + product_name)
print('Shipping: ' + shipping)
# actually writes it then replaces the commas with pipe bars
f.write(product_name.replace(',', '|') + ',' + shipping + '\n')
# closes the write file
f.close()
# closes the client
uClient.close()