1
andylsr 2018-05-13 22:07:26 +08:00 via Android
异常写的很清楚了,Chrome 类有一个属性 executable,这个是浏览器驱动,要么在类实例化的时候指定,要么在环境变量中添加~~
|
2
youngxu 2018-05-13 22:43:51 +08:00 via Android
去搜一下 selenium+chrome...要用 chromedriver 的
|
3
zeyexe 2018-05-13 23:11:24 +08:00
到 http://chromedriver.chromium.org/downloads 下载 chromedriver,然后把 chromedriver 的路径加到 PATH 中,或者在代码中直接指定。
|
4
kmahyyg 2018-05-14 01:07:01 +08:00 via iPad
老铁,发帖之前看看文档
|
5
baday 2018-05-14 09:45:20 +08:00
1、指定下载的浏览器驱动的地址。
2、如果不加参数就把驱动地址加到环境变量。 |
6
towl 2018-05-14 10:03:16 +08:00
写的很清楚了啊,没有设置 chromedriver 的环境变量
|
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() |
8
mimvp 2018-05-14 16:54:17 +08:00
|