rezepte minen klappt fast

einige ingred sind links, werden noch nicht ordentlich heruasgefiltert
This commit is contained in:
Askill 2020-04-06 22:23:08 +02:00
parent 5be395a113
commit 0992a8a702
2 changed files with 31 additions and 8 deletions

1
data/recs.json Normal file

File diff suppressed because one or more lines are too long

36
mine.py
View File

@ -64,34 +64,56 @@ def getRecipe(links):
tree = html.fromstring(site.content) tree = html.fromstring(site.content)
namePath = "/html/body/main/article[1]/div/div[2]/h1/text()" namePath = "/html/body/main/article[1]/div/div[2]/h1/text()"
ingredPath = "/html/body/main/article[2]/table/tbody/tr/td/span/text()" # TODO: fix this ingredPath = "/html/body/main/article[2]/table/tbody/tr/td" # TODO: fix this
recipPath = "/html/body/main/article[3]/div[1]/text()" recipPath = "/html/body/main/article[3]/div[1]/text()"
name = tree.xpath(namePath)[0] name = tree.xpath(namePath)[0]
ingred = tree.xpath(ingredPath) ingred = tree.xpath(ingredPath)
resip = tree.xpath(recipPath) resip = tree.xpath(recipPath)
resString = "" resString = ""
for x in resip: for x in resip:
resString += x + "\n\n" resString += x + "\n"
ingredDict = dict() ingredDict = {}
for i in range(0, len(ingred)-1, 2): for i in range(0, len(ingred)-1, 2):
ingredDict[ingred[i+1]] = ingred[i] #print(ingred[i+1][0].text)
recs[name] = [resString, ingredDict] if ingred[i+1][0].text is not None:
if ingred[i+1][0].text is None:
stuff = ingred[i+1][0][0].text.strip().replace(" ", "")
else:
stuff = ingred[i+1][0].text.strip().replace(" ", "")
else:
stuff = ""
if ingred[i][0].text is not None:
if ingred[i][0].text is None:
amount = ingred[i][0][0].text.strip().replace(" ", "")
else:
amount = ingred[i][0].text.strip().replace(" ", "")
else:
amount = ""
ingredDict[stuff] = amount
recs[name] = [resString, ingredDict]
print("") print("")
except Exception as e: except Exception as e:
print(e) print(e)
sleep(10)
print(link)
sleep(random.randint(0, 5)) sleep(random.randint(0, 5))
return recs
#links = getLinks() #links = getLinks()
#with open('./data/links.json', 'w') as file: #with open('./data/links.json', 'w') as file:
# jsonString = json.dumps(links) # jsonString = json.dumps(links)
# file.write(jsonString) # file.write(jsonString)
links = ""
with open('./data/links.json') as file: with open('./data/links.json') as file:
links = json.load(file) links = json.load(file)
getRecipe(links)
recs = getRecipe(links)
with open('./data/recs.json', 'w') as file:
jsonString = json.dumps(recs)
file.write(jsonString)