-
Notifications
You must be signed in to change notification settings - Fork 0
/
websites.py
144 lines (127 loc) · 4.35 KB
/
websites.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
from bs4 import BeautifulSoup
from const import REQUEST_HEADER, REQUEST_COOKIES, REMOVE_FROM_DOMAIN
class Websites:
@staticmethod
def Komplett(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.find("h1", itemprop="name").span.text
price = (
soup.find("span", class_="product-price-now")
.text.strip(",-")
.replace(".", "")
)
return name, price
@staticmethod
def Proshop(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.find("meta", property="og:title")["content"]
try:
# find normal price
price = (
soup.find("span", class_="site-currency-attention")
.text.split(",")[0]
.replace(".", "")
)
except AttributeError:
try:
# find discount price
price = (
soup.find(
"div", class_="site-currency-attention site-currency-campaign"
)
.text.split(",")[0]
.replace(".", "")
)
except AttributeError:
# if campaign is sold out (udsolgt)
price = (
soup.find("div", class_="site-currency-attention")
.text.split(",")[0]
.replace(".", "")
)
return name, price
@staticmethod
def AvXperten(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.find("meta", property="og:title")["content"]
price = soup.find("div", class_="price").text.replace(u"\xa0DKK", "")
return name, price
@staticmethod
def Computersalg(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.findAll("td", class_="specLine")[1].text
price = (
soup.find("span", itemprop="price")
.text.strip()
.split(",")[0]
.replace(".", "")
)
return name, price
@staticmethod
def Elgiganten(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.find("h1", class_="product-title").text
price = (
soup.find("div", class_="product-price-container")
.text.strip()
.replace(u"\xa0", "")
)
return name, price
@staticmethod
def Amazon(link: str):
# get name and price of product at link
soup = request_link(link)
name = soup.find("span", id="productTitle").text.strip()
price = (
soup.find("span", id="priceblock_ourprice")
.text.replace("$", "")
.split(".")[0]
.replace(",", "")
)
return name, price
@staticmethod
def eBay(link: str):
# get name and price of product at link
soup = request_link(link)
if link.split("/")[3] == "itm":
name = link.split("/")[4].replace("-", " ")
price = (
soup.find("span", id="convbinPrice")
.text.replace("(including shipping)", "")
.replace("DKK ", "")
.replace(",", "")
)
else:
name = soup.find("h1", class_="product-title").text
price = (
soup.find("div", class_="display-price")
.text.replace("DKK ", "")
.replace(",", "")
)
return name, price
GET_WEBSITE_METHOD = {
"komplett": Websites.Komplett,
"proshop": Websites.Proshop,
"avxperten": Websites.AvXperten,
"computersalg": Websites.Computersalg,
"elgiganten": Websites.Elgiganten,
"amazon": Websites.Amazon,
"ebay": Websites.eBay,
}
def request_link(link: str) -> BeautifulSoup:
response = requests.get(link, headers=REQUEST_HEADER, cookies=REQUEST_COOKIES)
soup = BeautifulSoup(response.text, "html.parser")
return soup
def get_domain_from_link(link: str) -> str:
# get domain with "www." and ".com"
domain = link.split("/")[2]
# remove "www." and ".com"
for to_remove in REMOVE_FROM_DOMAIN:
domain = domain.replace(to_remove, "")
return domain