0
ここで初心者の質問は申し訳ありませんが、私はYelpのEat24.comサイトで作業してウェブスクレイピングを学ぼうとしています。私は1)eat24.comへのドライバの取得、2)ピックアップの選択、3)場所の検索、4)最初のメニューのクリック、5)メニュー項目の収集ができます。しかし、元のレストランのリストに戻ってリストの次のメニューを選択することはできません。ここに私のコードは次のとおりです。問題はここで終わりですセレン - リスト内のすべての項目から情報を収集
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
#go to eat24, type in zip code 10007, choose pickup and click search
driver.get("https://new-york.eat24hours.com/restaurants/index.php")
search_area = driver.find_element_by_name("address_auto_complete")
search_area.send_keys("10007")
pickup_element = driver.find_element_by_xpath("//[@id='search_form']/div/table/tbody/tr/td[2]")
pickup_element.click()
search_button = driver.find_element_by_xpath("//*[@id='search_form']/div/table/tbody/tr/td[3]/button")
search_button.click()
#scroll up and down on page to load more of 'infinity' list
for i in range(0,3):
driver.execute_script("window.scrollTo(0,
document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0,0);")
time.sleep(1)
#find menu buttons
menus_elements = driver.find_elements_by_xpath('//*[@title="View Menu"]')
#menus_element = driver.find_element_by_xpath('//*[@title="View Menu"]')
#menus_element.click()
#Problem area: Trying to iterate over menu buttons and collect menu items + prices from each. It goes to the first menu and pulls the prices/menu items, but then when it goes back to first page it says 'stale element reference' and doesn't click the next menu item
for i in range(0, len(menus_elements)):
if menus_elements[i].is_displayed():
menus_elements[i].click()
#find menu items
menu_items = driver.find_elements_by_class_name("cpa")
menus = [x.text for x in menu_items]
#find menu prices
menu_prices = driver.find_elements_by_class_name('item_price')
menu_prices = [x.text for x in menu_prices]
#pair menu items and prices
for menu, menu_price in zip(menus, menu_prices):
print(menu + ': ' + menu_price)
driver.execute_script("window.history.go(-1)")
driver.implicitly_wait(20)
、それは最初のメニューに行くとアイテム/価格をつかむが、それは裏ページに行くとき、それは2番目のメニューを選択しないと同じことをしてください。どうして?すべてのアドバイスをありがとう!
はありがとうございました:)それは働いた。 –