-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (48 loc) · 2.1 KB
/
main.py
File metadata and controls
65 lines (48 loc) · 2.1 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
56
57
58
59
60
61
62
63
64
65
import requests
from bs4 import BeautifulSoup
import pandas as pd
import os
import webbrowser
f = open("output.csv", "w")
f.truncate()
f.close()
brands = ('drops', 'stylecraft', 'dmc', 'drops', 'hahn')
products = ('drops-baby-merino-mix', 'stylecraft-special-dk', 'dmc-natura-xl', 'drops-safran', 'alpacca-speciale')
dict_products = { i : products[i] for i in range(0, len(products) ) }
for brand, product in zip(brands, products):
website_url = requests.get('https://www.wollplatz.de/wolle/' + brand + '/' + product).text
r = requests.get('https://www.wollplatz.de/wolle/' + brand + '/' + product)
print(r.status_code)
if r.status_code != 404:
soup = BeautifulSoup(website_url,'html.parser')
if (soup.find('div', {"class" : 'buy-extrainfo'})) != None:
old_price = soup.find('span', {'class': 'product-price-amount'})
print('Old price is ' + old_price.text)
price = soup.find('span', {'class' : 'product-price'})
new_price = price.find('span' , {'class' : 'product-price-amount'})
print('New price ' + new_price.text)
price_dict = {'Price': new_price.text}
table_div = soup.find("div", {"id": "pdetailTableSpecs"})
table = table_div.find("table")
table_rows = table.find_all('tr')
data = {}
for tr in table_rows:
td = tr.find_all('td')
if len(td) != 2 : continue
data[td[0].text] = td[1].text.strip()
res = dict((k, data[k]) for k in ['Zusammenstellung' , 'Nadelstärke'] if k in data)
res.update(price_dict)
product_names = {'Brand': product}
res.update(product_names)
data_frame = pd.DataFrame([res], columns=res.keys())
data_frame.to_csv('output.csv', mode='a', index=False, header=False)
else:
print("site not found")
result = pd.read_csv('output.csv', names=["Zusammenstellung", "Nadelstärke", "Price", "Brand"] )
result = result.set_index("Brand")
html = result.to_html()
path = os.path.abspath('result.html')
url = 'file://' + path
with open(path, 'w') as f:
f.write(html)
webbrowser.open(url)