【问题标题】:WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/homeWebDriverException:消息:“chromedriver.exe”可执行文件需要在 PATH 中。请参阅 https://sites.google.com/a/chromium.org/chromedriver/home
【发布时间】:2020-11-30 02:37:53
【问题描述】:

以下代码:

#################################
# Simple-Instagram-Scraper v1.0.3
# Release: 10.11.2020
# GitHub: do-me
#################################

from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
import re
from urllib.request import urlopen
import json
from pandas.io.json import json_normalize
import pandas as pd, numpy as np
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import random 
import string
from time import gmtime, strftime
import os

option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}

# Parameters

#---------------------------------------------------------------

# CRUCIAL PARAMS

# directory to chromedriver or geckodriver, find chromedriver here: https://chromedriver.chromium.org/
browser = webdriver.Chrome("yourpath/chromedriver.exe", options=option)

# user credentials
username = "username"
userpassword = "userpassword"

# which page?
pagetoscrape = "https://www.instagram.com/explore/locations/118546/thessaloniki/" # either hashtag, location id or user account possible
# pagetoscrape = "https://www.instagram.com/explore/tags/berlin/"

#---------------------------------------------------------------

# OPTIONAL PARAMS

# maximum posts to scrape
maxiter = 10000

# filename for output csv
out_csv = "-".join(pagetoscrape.split("/")[-3:])[:-1] + ".csv" # i.e.'118546-thessaloniki' for locations or tags-berlin for hashtags

# quite crucial but subject to trial and error due to unknown Instagram blocking policy: breaks
# set a random break duration for every iteration after opening one post and before going to the next one
short_pauseduration_min = 2 # seconds 
short_pauseduration_max = 2.7 # seconds

# set a random break duration for longer breaks...
long_pauseduration_min = 4.8 # seconds 
long_pauseduration_max = 10.5 # seconds

# ...for the following random iterations (number of iterations = index of scraped posts)
pauselist = random.sample(range(10, 10000), 400) # between 10 and 10000 generate list of 500 values # randomList.sort() for sorting
pauselist.append([x + int(random.uniform(1,10)) for x in np.arange(20, 10000, 50).tolist()]) # just to make sure: add list with values every 51th to 60th iteration  

#---------------------------------------------------------------

# ADVANCED PARAMS

# uncomment for saving in a folder called "data" you created before manually
# if os.getcwd().split("\\")[-1] != "data": # if you changed working dir already for some reason
#  os.chdir(r"data") 

我收到以下错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:

~/anaconda3/lib/python3.7/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    799                                 errread, errwrite,
--> 800                                 restore_signals, start_new_session)
    801         except:

~/anaconda3/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1550                             err_msg += ': ' + repr(err_filename)
-> 1551                     raise child_exception_type(errno_num, err_msg, err_filename)
   1552                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'yourpath/chromedriver.exe': 'yourpath/chromedriver.exe'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
<ipython-input-3-943542c00cca> in <module>
     34 
     35 # directory to chromedriver or geckodriver, find chromedriver here: https://chromedriver.chromium.org/
---> 36 browser = webdriver.Chrome("yourpath/chromedriver.exe", options=option)
     37 
     38 # user credentials

~/anaconda3/lib/python3.7/site-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:

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py in start(self)
     81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
---> 83                         os.path.basename(self.path), self.start_error_message)
     84                 )
     85             elif err.errno == errno.EACCES:

WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

这里有两个问题:

  1. 如何强制 Jupyter notebook 使用 Chrome 而不是 Firefox?
  2. 如何在 Ubuntu 20.04 中修复此错误?

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver jupyter


    【解决方案1】:

    您的第一个问题:How to change the default browser used by the ipython/jupyter notebook in Linux?

    对于 2:

    它不需要在你的路径上。如果您注意到,它实际上是在告诉您它没有在您提供的路径中找到 chromedriver,因为那里没有文件。给它一个工作路径,你会没事的。

    【讨论】:

    • 谢谢,这是可执行文件所在的位置 ~/Downloads/chromedriver_linux64.zip/chromedriver/chromedriver_linux64/chromedriver 不确定如何处理它。您能否将您的答案扩展为 2. 并帮我弄清楚该怎么做?
    • ~ 在这种情况下不能用作路径字符串。给出一个绝对路径(以 / 开头)或当前目录的相对路径
    • 我该怎么做?你能更具体一点吗? Either give an absolute (starting with /) or a relative path from your current directory
    • 这是一个例子。如果您在 Windows 上,绝对路径将类似于 "C:/Users/username/Downloads/..../chromedriver.exe"。在 Linux 上,它看起来像 "/usr/Downloads/..../chromedriver"。我不确定我还能提供多少帮助,您可以找到
    • 您还在程序中将路径作为参数传递给它,因此将其添加到系统 PATH 将被覆盖
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多