爬虫-selenium的使用

安装

pip install selenium

 开始

# coding=utf-8
from selenium import webdriver  # 引用selenium库
import time

# 实例化一个浏览器
driver = webdriver.Chrome()
# 请求网页地址
driver.get("http://www.baidu.com")
# 元素定位方法
driver.find_element_by_id("kw").send_keys("python")
driver.find_element_by_id("su").click()

# 退出浏览器
time.sleep(3)
driver.quit()

报错

raceback (most recent call last):
  File "D:/python_work/18-20爬虫代码V3.1/爬虫代码V3.1/day06/code/01_try_selenium.py", line 6, in <module>
    driver = webdriver.Chrome()
  File "D:\Programs\Python\Python35\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "D:\Programs\Python\Python35\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

原因:没有安装chromedriver

chromedriver下载地址:https://chromedriver.storage.googleapis.com/index.html

解压缩

unzip chromedriver_linux64.zip

把文件夹移动到/usr/bin目录下

sudo mv chromedriver /usr/bin/

测试是否成功

 继续运行代码,成功

猜你喜欢

转载自www.cnblogs.com/yifengs/p/11746806.html
今日推荐