V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xfw
V2EX  ›  Python

小白求助, Python 3.6.1 环境, selenium 执行定义浏览器异常

  •  
  •   xfw · 2018-05-13 21:59:56 +08:00 · 3927 次点击
    这是一个创建于 2386 天前的主题,其中的信息可能已经有所发展或是发生改变。
    只有一个语句……
    browser = webdriver.Chrome()

    C:\python\autosign\venv\Scripts\python.exe C:/python/autosign/main.py
    Traceback (most recent call last):
    File "C:\python\autosign\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
    File "C:\Users\yuzhe\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
    File "C:\Users\yuzhe\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
    FileNotFoundError: [WinError 2] 系统找不到指定的文件。

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:/python/autosign/main.py", line 15, in <module>
    browser = webdriver.Chrome()
    File "C:\python\autosign\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
    self.service.start()
    File "C:\python\autosign\venv\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
    selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home


    Process finished with exit code 1
    8 条回复    2018-05-14 16:54:17 +08:00
    andylsr
        1
    andylsr  
       2018-05-13 22:07:26 +08:00 via Android
    异常写的很清楚了,Chrome 类有一个属性 executable,这个是浏览器驱动,要么在类实例化的时候指定,要么在环境变量中添加~~
    youngxu
        2
    youngxu  
       2018-05-13 22:43:51 +08:00 via Android
    去搜一下 selenium+chrome...要用 chromedriver 的
    zeyexe
        3
    zeyexe  
       2018-05-13 23:11:24 +08:00
    http://chromedriver.chromium.org/downloads 下载 chromedriver,然后把 chromedriver 的路径加到 PATH 中,或者在代码中直接指定。
    kmahyyg
        4
    kmahyyg  
       2018-05-14 01:07:01 +08:00 via iPad
    老铁,发帖之前看看文档
    baday
        5
    baday  
       2018-05-14 09:45:20 +08:00
    1、指定下载的浏览器驱动的地址。
    2、如果不加参数就把驱动地址加到环境变量。
    towl
        6
    towl  
       2018-05-14 10:03:16 +08:00
    写的很清楚了啊,没有设置 chromedriver 的环境变量
    mimvp
        7
    mimvp  
       2018-05-14 16:52:57 +08:00
    根据提示信息,找不到 chrome 执行文件,需要下载 chromedriver,然后添加到 executable_path=chromedriver
    chromedriver 下载地址: http://chromedriver.storage.googleapis.com/index.html

    推荐填写完整的绝对路径,例如:
    chromedriver = '/usr/local/bin/chromedriver'
    browser = webdriver.Chrome(executable_path=chromedriver) # 打开 Chrome 浏览器


    给出的完整代码如下:

    from selenium import webdriver
    from selenium.webdriver.common.proxy import *
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    from pyvirtualdisplay import Display
    # from xvfbwrapper import Xvfb

    import bs4, os, re, time, zipfile
    from base64 import b64encode

    import sys
    from posix import unlink
    reload(sys)
    sys.setdefaultencoding('utf8')


    ## webdriver + chrome (不使用代理,爬取网页)
    def spider_url_chrome(url):
    browser = None
    display = None
    try:
    display = Display(visible=0, size=(800, 600))
    display.start()
    chromedriver = '/usr/local/bin/chromedriver'
    browser = webdriver.Chrome(executable_path=chromedriver) # 打开 Chrome 浏览器
    browser.get(url)
    content = browser.page_source
    print("content: " + str(content))
    finally:
    if browser: browser.quit()
    if display: display.stop()


    ## webdriver + chrome + proxy + whiteip (无密码,或白名单 ip 授权)
    ## 米扑代理: https://proxy.mimvp.com
    def spider_url_chrome_by_whiteip(url):
    browser = None
    display = None

    ## 白名单 ip,请见米扑代理会员中心: https://proxy.mimvp.com/usercenter/userinfo.php?p=whiteip
    mimvp_proxy = {
    'ip' : '140.143.62.84', # ip
    'port_https' : 62288, # http, https
    'port_socks' : 62287, # socks5
    'username' : 'mimvp-user',
    'password' : 'mimvp-pass'
    }

    try:
    display = Display(visible=0, size=(800, 600))
    display.start()

    chrome_options = Options() # ok
    chrome_options = webdriver.ChromeOptions() # ok
    proxy_https_argument = '--proxy-server=http://{ip}:{port}'.format(ip=mimvp_proxy['ip'], port=mimvp_proxy['port_https']) # http, https (无密码,或白名单 ip 授权,成功)
    chrome_options.add_argument(proxy_https_argument)
    # proxy_socks_argument = '--proxy-server=socks5://{ip}:{port}'.format(ip=mimvp_proxy['ip'], port=mimvp_proxy['port_socks']) # socks5 (无密码,或白名单 ip 授权,失败)
    # chrome_options.add_argument(proxy_socks_argument)

    chromedriver = '/usr/local/bin/chromedriver'
    browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options) # 打开 Chrome 浏览器
    browser.get(url)
    content = browser.page_source
    print("content: " + str(content))
    finally:
    if browser: browser.quit()
    if display: display.stop()
    mimvp
        8
    mimvp  
       2018-05-14 16:54:17 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   937 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:25 · PVG 06:25 · LAX 14:25 · JFK 17:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.