Python selenium chromedriver 错误?WebDriver.__init__() 得到了意外的关键字参数“executable_path”

Y0h*_*hno 1 automation typeerror selenium-chromedriver selenium-webdriver

所以我对编码相当陌生,我应该解析 Yelp 评论,这样我就可以使用 Pandas 分析数据。我一直在尝试使用 selenium/beautifulsoup 来自动化整个过程,但我无法克服我编写的每个版本的代码中的 webdriver/chromedriver 错误。

!pip install selenium
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import os

# Set the path to the ChromeDriver executable
chromedriver_path = "C:\\Users\\5mxz2\\Downloads\\chromedriver\\chromedriver"

# Set the URL of the Yelp page you want to scrape
url = "https://www.yelp.com/biz/gelati-celesti-virginia-beach-2"

# Set the options for Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")  # Run Chrome in headless mode, comment this line if you want to see the browser window

# Create the ChromeDriver instance
driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)

# Load the Yelp page
driver.get(url)

# Extract the page source and pass it to BeautifulSoup
soup = BeautifulSoup(driver.page_source, "html.parser")

# Find all review elements on the page
reviews = soup.find_all("div", class_="review")

# Create empty lists to store the extracted data
review_texts = []
ratings = []
dates = []

# Iterate over each review element
for review in reviews:
    # Extract the review text
    review_text = review.find("p", class_="comment").get_text()
    review_texts.append(review_text.strip())

    # Extract the rating
    rating = review.find("div", class_="rating").get("aria-label")
    ratings.append(rating)

    # Extract the date
    date = review.find("span", class_="rating-qualifier").get_text()
    dates.append(date.strip())

# Create a DataFrame from the extracted data
data = {
    "Review Text": review_texts,
    "Rating": ratings,
    "Date": dates
}
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

# Get the current working directory
path = os.getcwd()

# Save the DataFrame as a CSV file
csv_path = os.path.join(path, "yelp_reviews.csv")
df.to_csv(csv_path, index=False)

# Close the ChromeDriver instance
driver.quit()

Run Code Online (Sandbox Code Playgroud)

这就是我到目前为止所拥有的,但我不断收到此错误消息

TypeError                                 Traceback (most recent call last)
<ipython-input-4-5712027ca0bf> in <cell line: 18>()
     16 
     17 # Create the ChromeDriver instance
---> 18 driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)
     19 
     20 # Load the Yelp page

TypeError: WebDriver.__init__() got an unexpected keyword argument 'executable_path'
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?如果有人对整个任务有任何建议,请告诉我。

Mic*_*ntz 5

这是由于以下更改所致selenium 4.10.0https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

请注意,该内容executable_path已被删除。

如果你想传递一个executable_path,你现在必须使用servicearg 。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path='./chromedriver.exe')
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
Run Code Online (Sandbox Code Playgroud)