-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTML_.py
55 lines (48 loc) · 1.83 KB
/
HTML_.py
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 bs4 import BeautifulSoup
import requests
DEFAULT_URL = "https://www.microcenter.com/search/search_results.aspx?Ntk=all&sortby=match&N=4294808776&myStore=false&storeid=101&rpp=96"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
def get_html(url: str):
page = requests.get(url, headers=headers).text
return BeautifulSoup(page, "html.parser")
def get_html_titles(doc: BeautifulSoup):
titles = []
details = doc.find_all(class_ = "detail_wrapper")
for detail in details:
titles.append(detail.find("a").string)
return titles
def get_html_prices(doc: BeautifulSoup):
prices = []
tags = doc.find_all(text="$")
for tag in tags:
prices.append(float(tag.parent.parent.text.replace(",","").strip("$")))
return prices
def get_html_stocks(doc: BeautifulSoup):
stocks = []
tags = doc.find_all(class_ = "stock")
for tag in tags:
text = tag.text.replace("IN STOCK", "").replace("SOLD OUT", "").replace("at Tustin Store", "").replace("+", "").replace("Buy In Store", "").replace("-", "").strip()
if len(text) > 0:
stocks.append(int(text))
else:
stocks.append(int(0))
return stocks
def get_html_SKU(doc: BeautifulSoup):
SKUs = []
tags = doc.find_all(class_ = "sku")
for tag in tags:
text = tag.text.replace("SKU: ", "")
if len(text) > 0:
SKUs.append(text)
else:
SKUs.append("unknow")
return SKUs
def get_html_link(doc: BeautifulSoup):
links = []
tags = doc.find_all(class_ = "detail_wrapper")
for tag in tags:
if len(tag) > 0:
links.append("https://www.microcenter.com" + tag.find("a").get("href"))
else:
links.append("unknow")
return links