'module' object is not callable error using webdriver in Google Collab to web scrape a site

'module' object is not callable error using webdriver in Google Collab to web scrape a site

我一直在尝试通过网络抓取酒店评论,但在多次页面跳转中,网页的 url 没有改变。所以我正在使用 selenium 的 webdriver 来解决这个问题。但是我一开始就不能在 google collab 中使用它。任何快速帮助将不胜感激。谢谢!

代码:

from selenium import webdriver
import requests
from bs4 import BeautifulSoup
import pandas as pd
# install chromium, its driver, and selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)

driver = webdriver.chrome()
driver.get("https://www.goibibo.com/hotels/highland-park-hotel-in-trivandrum-1383427384655815037/?hquery={%22ci%22:%2220211209%22,%22co%22:%2220211210%22,%22r%22:%221-2-0%22,%22ibp%22:%22v15%22}&hmd=766931490eb7863d2f38f56c6185a1308de782c89dfeeea59d262b827ca15441bf50472cbfdc1ee84aeed8af756809a2e89cfd6eaea0fa308c1ca839e8c313d016ac0f5948658353cf30f1cd83050fd8e6adb2e55f2a5470cadeb0c28b7becc92ac44d81966b82408effde826d40fbff47525e09b5f145e321fe6d104e12933c066323798e33a911e0cbed7312fc1634f8f92fe502c8602556c9a02f34c047d04ff1400c995799156776c1a04e218d6486493edad5b0f7e51a5ea25f5f1cb4f5ed497ee9368137f6ec73b3b1166ee7c1a885920b90c98542e0270b4fa9004005cfe87a4d1efeaedc8e33a848f73345f09bec19153e8bf625cc7f9216e692a1bcc313e7f13a7fc091328b1fb43598bd236994fdc988ab35e70cf3a5d1856c0b0fa9794b23a1a958a5937ac6d258d121a75b7ce9fc70b9a820af43a8e9a3f279be65b5c6fbfff2ba20bfb0f3e3ee425f0b930bf671c50878a540c6a9003b197622b6ab22ae39e07b5174cb12bebbcd2a132bb8570e01b9e253c1bd83cb292de97a&cc=IN&reviewType=gi&vcid=3877384277955108166&srpFilters={%22type%22:[%22Hotel%22]}")

错误:

当您发出命令时:

!pip install selenium

默认安装最新的 Selenium 4.1.0

最初这行代码:

wd = webdriver.Chrome('chromedriver',options=options)

启动 driven initiated 浏览上下文

但是下面一行代码:

driver = webdriver.chrome()

容易出错,因为 chrome() 是一个模块,而不是 可调用的 ,如:

from selenium.webdriver.chrome.options import Options

因此您看到错误:

'module' object is not callable

解决方案

初始行启动 / Chrome combo but with two 为:

您暂时可以忽略,但您需要删除代码行:

driver = webdriver.chrome()