[Python自学] 爬虫(5)selenium
一、准备工作
1.安装selenium
pip install selenium
2.下载安装chromedriver.exe
注意:如果Chrome在自动更新的话,可能会导致运行出现问题,我们应该去系统服务中禁用Chrome的自动更新服务。
下载chromedriver.exe:
http://chromedriver.storage.googleapis.com/index.html
下载与自己Chrome浏览器版本接近的版本(大版本号一定要对应上)。
可以按照以下方式查看版本是否能对应:
![[Python自学] 爬虫(5)selenium [Python自学] 爬虫(5)selenium](https://cdn.ancii.com/article/image/v1/sw/wV/kP/PkwwVsGDmjDG9swnOi7SkjVAsMQAarn73E9S3mmSmcA1C0KL_ve8eIMRn0lXD1WE9suBgi2XNUbq5nluFItEZA.png)
下载完毕后,将chromedrive.exe拷贝到Chrome浏览器根目录,以及python根目录下:
Chrome根目录:C:\Program Files (x86)\Google\Chrome\Application Python根目录:D:\Dev_apps\Anaconda5.3.0
以上是我的windows10的路径。
将Chrome根目录加到系统环境变量中:
![[Python自学] 爬虫(5)selenium [Python自学] 爬虫(5)selenium](https://cdn.ancii.com/article/image/v1/sw/wV/kP/PkwwVsGDmjDG9swnOi7SkjVAsMQAarn73E9S3mmSmcA1C0KL_ve8eIMRn0lXD1WEylX4MPnjRggIS5TGsAm7VA.png)
二、简单使用selenium
1.打开baidu首页
import time
from selenium import webdriver
# 创建一个Chrome浏览器实例
driver = webdriver.Chrome()
# 打开百度首页
driver.get("http://www.baidu.com")
# 睡眠5s观察效果
time.sleep(5)
# 退出浏览器
driver.quit()2.在百度首页自动搜索"python"
import time
from selenium import webdriver
# 创建一个Chrome浏览器实例
driver = webdriver.Chrome()
# 设置窗口大小为1920x1080
# driver.set_window_size(1920,1080)
# 设置窗口为全屏
driver.maximize_window()
# 打开百度首页
driver.get("http://www.baidu.com")
# 元素定位,找到百度的搜索输入框
driver.find_element_by_id("kw").send_keys(‘python‘)
# 点击搜索
driver.find_element_by_id("su").click()
# 睡眠5s观察效果
time.sleep(5)
# 退出浏览器
driver.quit()这样我们就完成了一次使用selenium来自动打开百度首页,并输入python进行搜索的流程。
===
相关推荐
xiangxiaojun 2020-09-23
letheashura 2020-08-14
王练 2020-07-18
xiangxiaojun 2020-06-25
Feastaw 2020-06-18
云之高水之远 2020-06-14
Reiki 2020-06-12
songerxing 2020-06-11
王练 2020-06-11
tiankele0 2020-06-09
云之高水之远 2020-06-05
tiankele0 2020-07-29
curiousL 2020-07-18
Reiki 2020-08-16