我正在嘗試使用 python 從網站上抓取資料。問題是:沒有安裝瀏覽器,無法安裝(是純Debian OS,沒有GUI)。我在想也許可以headless在 selenium 中使用 chrome 驅動程式和模式,但它似乎不起作用。
這是我的測驗代碼:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get('https://www.kino-teatr.ru/')
search_bar = driver.find_element_by_id('search_input_top') # find search bar
search_bar.send_keys('Avengers') # enter the name of the movie
search_bar.send_keys(Keys.ENTER) # get the results
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
div = soup.find('div', class_='list_item') # find the first item
print(div.find('a')['href']) # find a link to the page
它給了我以下錯誤
WebDriverException: Message: unknown error: cannot find Chrome binary
Stacktrace:
#0 0x5606b093c113 <unknown>
#1 0x5606b04046d8 <unknown>
#2 0x5606b04259c9 <unknown>
#3 0x5606b042319a <unknown>
#4 0x5606b045de0a <unknown>
#5 0x5606b0457f53 <unknown>
#6 0x5606b042dbda <unknown>
#7 0x5606b042eca5 <unknown>
#8 0x5606b096d8dd <unknown>
#9 0x5606b0986a9b <unknown>
#10 0x5606b096f6b5 <unknown>
#11 0x5606b0987725 <unknown>
#12 0x5606b096308f <unknown>
#13 0x5606b09a4188 <unknown>
#14 0x5606b09a4308 <unknown>
#15 0x5606b09bea6d <unknown>
#16 0x7f35ddc8bea7 <unknown>
我已經嘗試按照此處所述安裝驅動程式并按照此處所述安裝其他庫,但沒有成功。
是否可以在沒有安裝瀏覽器的情況下使用 selenium,我應該怎么做才能做到這一點?
提前感謝您的任何幫助或建議!
uj5u.com熱心網友回復:
您可以嘗試安裝 requests 庫并執行以下操作以獲取所需的 HTML 頁面:
>>> import requests
>>> url = 'https://www.geeksforgeeks.org'
>>> response = requests.get(url).text
>>> '7 Alternative Career Paths For Software Engineers' in response
True
然后你可以使用LXML或BeautifulSoup來決議頁面
更新
from lxml import html
response = requests.post('https://www.kino-teatr.ru/search/', data={'text':'мстители'.encode('cp1251')}).content
doc = html.fromstring(response)
entries = doc.xpath('//div[@]/h4')
first_movie = entries[0].text_content()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424662.html
標籤:Python 硒 硒网络驱动程序 硒铬驱动程序 网络驱动程序
