如何在合作Google上使用Selenium? [英] How to use Selenium on Colaboratory Google?
本文介绍了如何在合作Google上使用Selenium?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从网络上抓取了很多信息,希望它可以在云上运行. 所以我想使用colaboratory,但是它变成了错误
I scraping many info from web, and I hope it works on cloud. So I'd like to use colaboratory, but it turned error
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中使用Selenium Webdriver .google.com? 但实际上不是.
I read the articles, and it says this works. How can we use Selenium Webdriver in colab.research.google.com? But actually not.
任何想法都值得赞赏.
我的选择是
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)
↑ 这最后一句话出错了
↑ this last sentence makes error
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6
============================================ 我的整个图表
============================================ My entire chart
!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脚本是
then, python script is
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
我将它们全部包装到一个库中
I wrap this all into a library
!pip install kora
from kora.selenium import wd
这篇关于如何在合作Google上使用Selenium?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文