TypeError: __init__() 使用带有公司 pac 文件的 Python Selenium ChromeDriver 时出现意外的关键字参数“service”错误

use*_*034 15 python selenium google-chrome selenium-chromedriver selenium-webdriver

我已经为这个问题苦苦挣扎了一段时间,但现在我又回到了这个问题上。我正在尝试使用 selenium 使用 pac 文件从公司代理后面的 URL 中抓取数据。我正在使用 Chromedriver,我的浏览器在其配置中使用 pac 文件。

我一直在尝试使用所需的功能,但文档很糟糕或者我没有掌握一些东西。最初,我尝试使用 beautifulsoup 进行网页抓取,除了我现在需要的数据是 javascript 格式的之外,我一直在使用它,而无法使用 bs4 读取该数据。

下面是我的代码:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()

PAC_PROXY = {
    'proxyAutoconfigUrl': 'http://proxy-pac/proxy.pac',
}
proxy = Proxy()
proxy.proxy_autoconfig_url = PAC_PROXY['proxyAutoconfigUrl']

desired_capabilities = {}
proxy.add_to_capabilities(desired_capabilities)
URL = "https://mor.nlm.nih.gov/RxClass/search?query=ALIMENTARY%20TRACT%20AND%20METABOLISM%7CATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4%7Catc%2Cepc%7Cdailymed%2Cmeshpa%7Cmesh%2Cdisease%7Cmedrt%2Cchem%7Cdailymed%2Cmoa%7Cdailymed%2Cpe%7Cdailymed%2Cpk%7Cmedrt%2Ctc%7Cfmtsme%2Cva%7Cva%2Cdispos%7Csnomedct%2Cstruct%7Csnomedct%2Cschedule%7Crxnorm"

service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get(URL)
print(driver.requests[0].headers, driver.requests[0].response)

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'tr.dbsearch')))
print(pd.read_html(driver.page_source)[1].iloc[:,:-1])
pd.read_html(driver.page_source)[1].iloc[:,:-1].to_csv('table.csv',index=False)
Run Code Online (Sandbox Code Playgroud)

我不确定为什么我收到:

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

即使我已将路径正确添加到系统环境变量中,如下所示:

在此输入图像描述

本质上我想做的是抓取表中的数据,然后将https://mor.nlm.nih.gov/RxClass/search?query=ALIMENTARY%20TRACT%20AND%20METABOLISM%7CATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4%7Catc%2Cepc%7Cdailymed%2Cmeshpa%7Cmesh%2Cdisease%7Cmedrt%2Cchem%7Cdailymed%2Cmoa%7Cdailymed%2Cpe%7Cdailymed%2Cpk%7Cmedrt%2Ctc%7Cfmtsme%2Cva%7Cva%2Cdispos%7Csnomedct%2Cstruct%7Csnomedct%2Cschedule%7Crxnorm其存储到 pandas 数据帧并将其传递到 csv 文件。

Deb*_*anB 18

如果您仍在使用Selenium v​​3.x,那么您不应该使用Service(),在这种情况下,关键的 executable_path是相关的。在这种情况下,代码行将是:

driver = webdriver.Chrome(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
Run Code Online (Sandbox Code Playgroud)

否则,如果您使用的是,那么您必须使用Service(),在这种情况下,关键的 executable_path不再相关。所以你需要改变这行代码:

service = Service(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)
Run Code Online (Sandbox Code Playgroud)

作为:

service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')
Run Code Online (Sandbox Code Playgroud)

  • 您好,Debanjan,谢谢您的回复。我正在使用硒 4.10。我已将行更新为 service = Service('C:\Program Files\Chrome Driver\chromedriver.exe'),但我仍然收到 TypeError: __init__() got an Unexpected keywords argument 'service'。上面的代码也已更新以显示更改。 (2认同)

小智 5

自从我从 pip Jupyter 切换到 Anaconda Jupyter 以来,我也一直遇到这个问题。这对我有用:

driver = webdriver.Chrome(ChromeDriverManager().install())

显然你不需要 Jupyter 包中的服务。