簡體   English   中英

如何在 Colaboratory Google 上使用 Selenium?

[英]How to use Selenium on Colaboratory Google?

我從網上抓取了很多信息,我希望它可以在雲上運行。 所以我想使用 colaboratory,但它變成了錯誤

WebDriverException                        Traceback (most recent call last)
<ipython-input-35-abcc3b93dfa7> in <module>()
     20 options.add_argument("--start-maximized");
     21 options.add_argument("--headless");
---> 22 driver = webdriver.Chrome('chromedriver', chrome_options=options)
     23 
     24 book  = cd + "/target.xlsx"

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
     96         count = 0
     97         while True:
---> 98             self.assert_process_still_running()
     99             if self.is_connectable():
    100                 break

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
    109             raise WebDriverException(
    110                 'Service %s unexpectedly exited. Status code was: %s'
--> 111                 % (self.path, return_code)
    112             )
    113 

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6

我閱讀了文章,它說這是有效的。 我們如何在 colab.research.google.com 中使用 Selenium Webdriver? 但實際上不是。

任何想法表示贊賞。

我的選擇是

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)

↑ 最后一句話有誤

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6

============================================ 我的整個圖表

!sudo apt install unzip
!wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
!unzip chromedriver_linux64.zip -d /usr/bin/
from google.colab import drive
drive.mount('/content/drive')
!pip install selenium
!pip install openpyxl

然后,python腳本是

cd = "drive/My Drive/doc/業務資料/イーコレ/scrap/*"
import os, subprocess
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
import selenium
import bs4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
import openpyxl
import time, re, csv, urllib.parse
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)
# install chromium, its driver, and selenium
!apt update
!apt install chromium-chromedriver
!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)
wd.get("https://www.website.com")
print(wd.page_source)  # results

我把這一切都包裝成一個圖書館

!pip install kora
from kora.selenium import wd

我認為這段代碼會起作用:

!sudo apt install unzip
!wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip
#!unzip chromedriver_linux64.zip -d /usr/bin/
from google.colab import drive
!pip install selenium
!pip install openpyxl
!apt-get update
!apt-get install -y unzip xvfb libxi6 libgconf-2-4
!apt-get install default-jdk 
cd = "drive/My Drive/doc/業務資料/イーコレ/scrap/*"
import os, subprocess
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
import selenium
import bs4
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
import openpyxl
import time, re, csv, urllib.parse
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome('chromedriver', chrome_options=options)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM