我一直在為這個問題苦苦掙扎,但現在我又回到了這個問題上。我正在嘗試使用 selenium 從使用 pac 檔案的公司代理后面的 URL 中抓取資料。我正在使用 Chromedriver,我的瀏覽器在其配置中使用 pac 檔案。
我一直在嘗試使用desired_capabilities,但檔案很糟糕,或者我沒有掌握一些東西。最初,我試圖用 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 TRACT AND METABOLISM|ATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4|atc,epc|dailymed,meshpa|mesh,disease|medrt,chem|dailymed,moa|dailymed,pe|dailymed,pk|medrt,tc|fmtsme,va|va,dispos|snomedct,struct|snomedct,schedule|rxnorm"
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)
我不確定為什么我會收到:
TypeError: __init__() got an unexpected keyword argument 'service'
即使我將路徑正確添加到我的系統環境變數中,如下所示:

本質上,我試圖做的是抓取表中的資料,https://mor.nlm.nih.gov/RxClass/search?query=ALIMENTARY TRACT AND METABOLISM|ATC1-4&searchBy=class&sourceIds=a&drugSources=atc1-4|atc,epc|dailymed,meshpa|mesh,disease|medrt,chem|dailymed,moa|dailymed,pe|dailymed,pk|medrt,tc|fmtsme,va|va,dispos|snomedct,struct|snomedct,schedule|rxnorm然后將其存盤到 Pandas 資料幀并將其傳遞給 csv 檔案。
uj5u.com熱心網友回復:
如果您仍在使用Selenium v3.x,那么您不應該使用Service()并且在這種情況下,關鍵的 executable_path是相關的。在這種情況下,代碼行將是:
driver = webdriver.Chrome(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
否則,如果您正在使用 硒4那么你必須使用Service(),在這種情況下,關鍵的 executable_path不再相關。所以你需要改變這行代碼:
service = Service(executable_path='C:\Program Files\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(service=service)
作為:
service = Service('C:\Program Files\Chrome Driver\chromedriver.exe')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402965.html
標籤:
