-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
76 lines (49 loc) · 1.98 KB
/
test.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
import csv
import unicodedata
driver = webdriver.Chrome()
"""
# Goes to the absolute bottom of the page
print("Scrolling to the bottom of the page")
#number of times the bot should scroll down
n = 3000
for i in range(n):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
print("Scrolled down {}/{} times".format(i+1, n))
time.sleep(3)
print("Scrolling done")
"""
n = 500
def unicode_to_ascii(txt):
return unicodedata.normalize("NFKD", txt).encode("ascii", "ignore")
with open("recipes.csv", "w+") as file:
csvWrite = csv.writer(file, delimiter=",")
for page_index in range(1,n+1):
driver.get("https://www.food2fork.com/index/" + str(page_index))
recipe_links = driver.find_elements_by_class_name("recipe-link")
length = len(recipe_links)
for index, link in enumerate(recipe_links):
print("On page {}/{} and recipe {}/{}".format(page_index, n+1, index+1, length))
# Opens new tab
url = link.get_attribute("href")
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).click(link).key_up(Keys.SHIFT).perform()
#driver.execute_script('''window.open("http://www.google.com","_blank");''')
tabs = driver.window_handles
# Opens the recipe link in the new tab and switches the driver to it
driver.switch_to.window(tabs[1])
driver.get(url)
# Finds recipe name
recipe_name = driver.find_elements_by_class_name("recipe-title")[0].text
ingredients = []
for ingredient in driver.find_elements_by_tag_name("li")[6:]:
ingredients.append(unicode_to_ascii(ingredient.text))
link = driver.find_element(By.XPATH, '//a[@target="_blank"]')
csvWrite.writerow([unicode_to_ascii(recipe_name), unicode_to_ascii(link.get_attribute("href")),ingredients])
driver.close()
driver.switch_to.window(tabs[0])
file.close()