-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
114 lines (93 loc) · 3.26 KB
/
logic.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
# import working libraries
import requests
from bs4 import BeautifulSoup
import json, re
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"}
def get_table(table):
thead = table.find("thead")
tbody = table.find("tbody")
label = thead.find("tr").find_all("th")[1].text.strip()
values = {}
for tr in tbody.find_all("tr"):
td = tr.find_all("td")
name = td[0].text.strip()
value = td[1].text.strip()
values.update({name:value})
return label, values
def scraper(src,html):
# convert the html file into bs4 object
page = BeautifulSoup(html,"html.parser")
# check if it has nutriscore word
if "nutriscore" in html.lower():
it_has_nutriscore = True
else:
it_has_nutriscore = False
# find script tag
try:
script = page.find("script",{"type":"application/ld+json"})
raw = json.loads(script.text)
except: raw = None
if raw != None:
# find product name
try: name = raw[2]['name']
except: name = None
# find product brand
try: brand = raw[2]['brand']['name']
except: brand = None
# find product image url
try: img_url = raw[2]['image'][0]
except: img_url = None
# find product categories
try: categories = "; ".join([e['item']['name'] for e in raw[3]['itemListElement'][1:]]) + ";"
except: categories = None
# find product barcode gtin
try: code = raw[2]['gtin13']
except: code = None
else:
name = brand = img_url = categories = code = None
# find product calories information
try:
product_info = page.find("div",{"class":["grocery-product","grocery-product__product-desc"]})
except:
product_info = None
try:
nutritions_table = product_info.find("section",{"class":"tabularContent"}).find("table")
label, values = get_table(nutritions_table)
try:
calories = " = ".join([label,values["-"]])
except:
calories = " = ".join([label,values["Energy"]])
except:
calories = None
# find product ingerdients information
try:
ingredients = product_info.find("div",{"class":"product-info-block","id":"ingredients"}).text.strip()
except:
ingredients = None
# find product origin information
try:
origin = product_info.find("div",{"class":"product-info-block","id":"manufacturer-address"}).find("ul").get_text()
except:
origin = None
product_data = {
'url': src,
'image_url': img_url,
'name': name,
'brand': brand,
'nutriscore': None,
'categories': categories,
'ingredients': ingredients,
'calories': calories,
'manufactured_at': origin,
"it_has_nutriscore": it_has_nutriscore,
'code': code
}
if len([v for v in product_data.values() if v == None]) != 9:
return product_data
else:
return None
if __name__ == "__main__":
url = "https://www.tesco.com/groceries/en-GB/products/303004400"
req = requests.get(url, headers=headers)
data = scraper(url, req.text)
print(data)