简体   繁体   English

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

[英]TypeError: __init__() got an unexpected keyword argument 'service' error using Python Selenium ChromeDriver with company pac file

I've been struggling with this problem for sometime, but now I'm coming back around to it.我一直在努力解决这个问题,但现在我又回到了这个问题上。 I'm attempting to use selenium to scrape data from a URL behind a company proxy using a pac file.我正在尝试使用 selenium 使用 pac 文件从公司代理后面的 URL 中抓取数据。 I'm using Chromedriver, which my browser uses the pac file in it's configuration.我正在使用 Chromedriver,我的浏览器在其配置中使用 pac 文件。

I've been trying to use desired_capabilities, but the documentation is horrible or I'm not grasping something.我一直在尝试使用desired_capabilities,但是文档很糟糕,或者我没有掌握一些东西。 Originally, I was attempting to webscrape with beautifulsoup, which I had working except the data I need now is in javascript, which can't be read with bs4.最初,我试图用 beautifulsoup 进行网络爬虫,除了我现在需要的数据在 javascript 中之外,我一直在使用它,这不能用 bs4 读取。

Below is my code:下面是我的代码:

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(executable_path='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)

I'm not sure why I'm receiving an:我不确定为什么我会收到:

TypeError: __init__() got an unexpected keyword argument 'service'

even when I have the path added correctly to my system environment variables as shown below:即使我将路径正确添加到我的系统环境变量中,如下所示:

在此处输入图像描述

Essentially what I'm attempting to do is scrape the data in the table from 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 then store it to a pandas dataframe and pass it to a csv file.基本上我想要做的是从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中抓取表中的数据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 dataframe 并将其传递给 csv 文件。

If you are still using Selenium v3.x then you shouldn't use the Service() and in that case the key executable_path is relevant.如果您仍在使用Selenium v3.x ,那么您不应该使用Service()并且在这种情况下, key executable_path是相关的。 In that case the lines of code will be:在这种情况下,代码行将是:

driver = webdriver.Chrome(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')

Else, if you are using then you have to use Service() and in that case the key executable_path is no more relevant.否则,如果您使用的是 ,那么您必须使用Service() ,在这种情况下, key executable_path不再相关。 So you need to change the line of code:因此,您需要更改代码行:

service = Service(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)

as:作为:

service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium TypeError: __init__() 得到了一个意外的关键字参数 'service' - Selenium TypeError: __init__() got an unexpected keyword argument 'service' Selenium Firefox:TypeError:__init__() 得到了一个意外的关键字参数“options” - Selenium Firefox : TypeError: __init__() got an unexpected keyword argument 'options' TypeError at '' __init__() 得到一个意外的关键字参数 '' - TypeError at '' __init__() got an unexpected keyword argument '' Python openpyxl TypeError:__init __()获得了意外的关键字参数'pivotButton' - Python openpyxl TypeError: __init__() got an unexpected keyword argument 'pivotButton' Telethon Python TypeError:__init__() 得到了一个意外的关键字参数“hash” - Telethon Python TypeError: __init__() got an unexpected keyword argument 'hash' Python Flask - TypeError: __init__() 得到了一个意外的关键字参数“lable” - Python Flask - TypeError: __init__() got an unexpected keyword argument 'lable' Python 3.5:TypeError:__ init __()得到了一个意外的关键字参数'nosigint' - Python 3.5 : TypeError: __init__() got an unexpected keyword argument 'nosigint' python请求:TypeError:__ init __()得到一个意外的关键字参数'proxies' - python requests: TypeError: __init__() got an unexpected keyword argument 'proxies' TypeError: __init__() 得到了一个意外的关键字参数“文件” - TypeError: __init__() got an unexpected keyword argument 'file' Scrapy错误:TypeError:__ init __()得到一个意外的关键字参数'deny' - Scrapy Error: TypeError: __init__() got an unexpected keyword argument 'deny'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM