这是我想要实现的工作流程: - 加载doordash.com - 输入地址 - 点击查找餐厅 - 滚动到页面末尾和所有餐馆的名称
我的脚本现在输入地址,似乎点击了查找餐厅按钮,但之后的页面由于某种原因没有加载
from sys import platform
import sys
import requests
from bs4 import BeautifulSoup
import os
from urllib import urlretrieve
import zipfile
import time
import os
import datetime
from datetime import datetime, timedelta
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
from selenium.common.exceptions import TimeoutException
import time
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class Scrapper:
def __init__(self):
self.profiles = {}
if platform == "linux" or platform == "linux2":
print "Fetching Linux Path Variables"
self.OS = "linux64"
elif platform == "darwin":
print "Fetching Mac Path Variables"
self.OS = "mac64"
elif platform == "win32":
print "Fetching Windows Path Variables"
self.OS = "win32"
if not os.path.isfile("driver/chromedriver"):
self.fetchChromeDriver(self.OS)
if platform == "linux" or platform == "linux2" or platform == "darwin":
os.system("chmod a+x driver/chromedriver")
self.chromedriverPath = "driver/chromedriver"
def fetchChromeDriver(self, OS):
print("Downloading Chromedriver")
self.createFolders("driver/")
localZipPath = "driver/{}.zip".format(OS)
urlretrieve("https://chromedriver.storage.googleapis.com/2.35/chromedriver_{}.zip".format(OS), localZipPath)
zip_ref = zipfile.ZipFile(localZipPath, 'r')
zip_ref.extractall("driver/")
zip_ref.close()
os.remove(localZipPath)
def createFolders(self, path):
if(not os.path.isdir(path)):
os.makedirs(path)
def downloadPage(self,url):
chrome_options = Options()
chrome_driver = self.chromedriverPath
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get(url)
elem = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[@placeholder='Enter your delivery address']")))
elem.send_keys('2 Pierce Ave, San Jose')
elem.send_keys(Keys.RETURN)
time.sleep(2)
elements = driver.find_elements_by_xpath("//button[contains(@class, 'Autocomplete_suggestionButton_')]")
elements[0].click()
button = driver.find_element(By.XPATH, '//button[text()="Find Restaurants"]')
button.click()
time.sleep(20)
def main():
scrapper = Scrapper()
scrapper.downloadPage('https://www.doordash.com/food-delivery/')
if __name__ == '__main__':
main()
答案 0 :(得分:0)
如果您在elem.send_keys(Keys.RETURN)
的定义中在time.sleep(2)
和elements = ...
之间添加了另一个downloadPage
,它就会转到下一页。
希望它适合你
答案 1 :(得分:0)
您只需等待“查找餐馆”按钮即可点击。这里:
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[text()="Find Restaurants"]')))
button.click()
祝你好运!