-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (114 loc) · 4.57 KB
/
Copy pathmain.py
File metadata and controls
152 lines (114 loc) · 4.57 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
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
import base64
import json
import os
import re
import string
import urllib.request
from string import digits
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
from ModelRecipe import ModelRecipe
debug = False
folderRecipes = "Recipes"
def saveRecipe(linkRecipeToDownload):
soup = downloadPage(linkRecipeToDownload)
title = findTitle(soup)
filePath = calculateFilePath(title)
if os.path.exists(filePath):
return
ingredients = findIngredients(soup)
description = findDescription(soup)
category = findCategory(soup)
imageBase64 = findImage(soup)
modelRecipe = ModelRecipe()
modelRecipe.title = title
modelRecipe.ingredients = ingredients
modelRecipe.description = description
modelRecipe.category = category
modelRecipe.imageBase64 = imageBase64
createFileJson(modelRecipe.toDictionary(), filePath)
def findTitle(soup):
titleRecipe = ""
for title in soup.find_all(attrs={"class": "gz-title-recipe gz-mBottom2x"}):
titleRecipe = title.text
return titleRecipe
def findIngredients(soup):
allIngredients = []
for tag in soup.find_all(attrs={"class": "gz-ingredient"}):
link = tag.a.get("href")
nameIngredient = tag.a.string
contents = tag.span.contents[0]
quantityProduct = re.sub(r"\s+", " ", contents).strip()
allIngredients.append([nameIngredient, quantityProduct])
return allIngredients
def findDescription(soup):
allDescription = ""
for tag in soup.find_all(attrs={"class": "gz-content-recipe-step"}):
removeNumbers = str.maketrans("", "", digits)
if hasattr(tag.p, "text"):
description = tag.p.text.translate(removeNumbers)
allDescription = allDescription + description
return allDescription
def findCategory(soup):
for tag in soup.find_all(attrs={"class": "gz-breadcrumb"}):
category = tag.li.a.string
return category
def findImage(soup):
# Find the first picture tag
pictures = soup.find("picture", attrs={"class": "gz-featured-image"})
# Fallback: find a div with class `gz-featured-image-video gz-type-photo`
if pictures is None:
pictures = soup.find(
"div", attrs={"class": "gz-featured-image-video gz-type-photo"}
)
imageSource = pictures.find("img")
# Most of the times the url is in the `data-src` attribute
imageURL = imageSource.get("data-src")
# Fallback: if not found in `data-src` look for the `src` attr
# Most likely, recipes which have the `src` attr
# instead of the `data-src` one
# are the older ones.
# As a matter of fact, those are the ones enclosed
# in <div> tags instead of <picture> tags (supported only on html5 and onward)
if imageURL is None:
imageURL = imageSource.get("src")
imageToBase64 = str(base64.b64encode(requests.get(imageURL).content))
imageToBase64 = imageToBase64[2 : len(imageToBase64) - 1]
return imageToBase64
def calculateFilePath(title):
compact_name = title.replace(" ", "_").lower()
return folderRecipes + "/" + compact_name + ".json"
def createFileJson(data, path):
with open(path, "w") as file:
file.write(json.dumps(data, ensure_ascii=False))
def downloadPage(linkToDownload):
response = requests.get(linkToDownload)
soup = BeautifulSoup(response.text, "html.parser")
return soup
def downloadAllRecipesFromGialloZafferano():
totalPages = countTotalPages() + 1
# for pageNumber in range(1,totalPages):
for pageNumber in tqdm(range(1, totalPages), desc="pages…", ascii=False, ncols=75):
linkList = "https://www.giallozafferano.it/ricette-cat/page" + str(pageNumber)
response = requests.get(linkList)
soup = BeautifulSoup(response.text, "html.parser")
for tag in soup.find_all(attrs={"class": "gz-title"}):
link = tag.a.get("href")
saveRecipe(link)
if debug:
break
if debug:
break
def countTotalPages():
numberOfPages = 0
linkList = "https://www.giallozafferano.it/ricette-cat"
response = requests.get(linkList)
soup = BeautifulSoup(response.text, "html.parser")
for tag in soup.find_all(attrs={"class": "disabled total-pages"}):
numberOfPages = int(tag.text)
return numberOfPages
if __name__ == "__main__":
if not os.path.exists(folderRecipes):
os.makedirs(folderRecipes)
downloadAllRecipesFromGialloZafferano()