-
Notifications
You must be signed in to change notification settings - Fork 0
/
price_tracker.py
166 lines (135 loc) · 4.85 KB
/
price_tracker.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from urllib.request import Request, urlopen
import re
from bs4 import BeautifulSoup
import pandas
import requests
from pyexcel_xlsx import get_data, save_data
def elgiganten_price(url):
url = Request(
url,
headers={'User-Agent': 'Mozilla/5.0'}
)
webpage = urlopen(url).read()
soup = BeautifulSoup(webpage, 'html.parser')
price = str(soup.find('div', {"class": "product-price-container"}))
price = re.sub("[^0-9]", "", price)
return price
def netonnet_price(url):
url = Request(
url,
headers={'User-Agent': 'Mozilla/5.0'}
)
webpage = urlopen(url).read()
soup = BeautifulSoup(webpage, 'html.parser')
price = str(soup.find('div', {"class": "price-big"}))
price = re.sub("[^0-9]", "", price)
return price
def webhallen_price(url):
with requests.Session() as session:
response = session.get(url).json()
price = response["product"]["price"]["price"]
return price
def amazon_de_price(url):
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
"Accept-Encoding":"gzip, deflate",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"DNT":"1",
"Connection":"close",
"Upgrade-Insecure-Requests":"1"
}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
price = soup.find(id="priceblock_ourprice").get_text()
price = price[ 0 : price.index(".")]
price = re.sub("[^0-9]", "", price)
price = round(int(price) * 10.15)
return price
def amazon_sv_price(url):
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
"Accept-Encoding":"gzip, deflate",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"DNT":"1",
"Connection":"close",
"Upgrade-Insecure-Requests":"1"
}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
price = soup.find(id="priceblock_ourprice").get_text()
price = price[ 0 : price.index(",")]
price = re.sub("[^0-9]", "", price)
return price
def data():
product_url = []
i = 0
excel_data_df = pandas.read_excel('Data.xlsx', sheet_name='Sheet1')
while i < len(excel_data_df.columns):
column = excel_data_df.columns[i]
selected_data = excel_data_df[column].tolist()
product_url.append(selected_data[0])
i += 1
return product_url, excel_data_df.columns.ravel(), excel_data_df
def get_price(urls, companies):
product_price = []
for url in urls:
if companies[urls.index(url)] == 'elgiganten':
try:
product_price.append(elgiganten_price(url))
except:
product_price.append("null")
elif companies[urls.index(url)] == 'webbhallen':
#try:
product_price.append(webhallen_price(url))
#except:
#product_price.append("null")
elif companies[urls.index(url)] == 'netonnet':
try:
product_price.append(netonnet_price(url))
except:
product_price.append("null")
elif companies[urls.index(url)] == 'amazon_de':
try:
product_price.append(amazon_de_price(url))
except:
product_price.append("null")
elif companies[urls.index(url)] == 'amazon_sv':
try:
product_price.append(amazon_sv_price(url))
except:
product_price.append("null")
elif companies[urls.index(url)] == '0':
product_price.append('Error with formating in Excel (no header): {}'.format(url))
return product_price
def write_data(product_price, excel_data, companies):
rows, columns = excel_data.shape
j = 0
i = 0
new_data = []
new_data.append([])
nested_list = new_data[0]
for companies_name in companies:
nested_list.append(companies_name)
while i < rows:
new_data.append([])
nested_list = new_data[i+1]
while j < columns:
column = excel_data.columns[j]
selected_data = excel_data[column].tolist()
nested_list.append(selected_data[i])
j+=1
j=0
i+=1
new_data.append([])
nested_list = new_data[-1]
for item in product_price:
nested_list.append(item)
data = get_data("Data.xlsx")
data.update({"Sheet1": new_data})
save_data("Data.xlsx", data)
def main():
PRODUCT_URL, COMPANIES, EXCEL_DATA = data()
PRODUCT_PRICE = get_price(PRODUCT_URL, COMPANIES)
write_data(PRODUCT_PRICE, EXCEL_DATA, COMPANIES)
if __name__ == '__main__':
main()