转自:https://zhuanlan.zhihu.com/p/581264235
作为通过模拟鼠标在浏览器上操作的python库,selenium有着简单易学,清晰的可视化(driver本身是一个可视的浏览器),能够快速布置等特点,非常适合初学者上手。
本文是笔者平常做研究获取数据过程中的使用经验和遇到的坑的一些总结,希望能帮到大家。但是由于使用经验仅限于对文本数据的获取,所以本文代码只会涉及获取字符串数据。
所有代码基于python。所有代码基于Chrome driver。
一、安装及配置
官方文档:The Selenium Browser Automation Project
selenium-python中文文档: Selenium with Python中文翻译文档
要注意的是selenium的安装分为两部分:
selenium包的安装:以python为例就是正常的pip install selenium
和浏览器模拟器的下载:直接下载官网相应的driver,比如我本身使用Chrome浏览器所以下载Chrome Driver。注意一定要下载适配现在版本的driver,不然会无法运行。下载之后是一个压缩文件,直接解压缩就可以使用。driver文件的路径不要忘了,因为需要在代码中唤起。
Driver链接(其实也是官方文档):WebDriver
二、常用方法命令
载入selenium相关包和组件
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
# ignore warnings
import warnings
warnings.filterwarnings('ignore')
注意此处选择忽略警告,因为笔者喜欢使用xpath来定位,但是在selenium中使用xpath定位时会有warning(并不影响使用)。
1 初始化driver
首先初始化driver参数:
# initialize the Chrome driver
options = ChromeOptions()
options.add_argument('lang=en_US') # 设置语言,默认跟浏览器设置一样,答主这里需要英文网页所以改成英文
service = ChromeService(executable_path="~\chromedriver.exe")
打开driver,将driver的路径替换为自己的路径,运行后会弹出一个浏览器模拟器:
# open the simulated browser
driver = webdriver.Chrome(chrome_options = options, service=service)
但是当存在大量循环需求的时候,需要将图片加载禁用以提高网页加载速度,所以使用以下初始化设置(如果没有不加载图片的需求可以不设置):
# initialize the Chrome driver
prefs = {'profile.managed_default_content_settings.images': 2} # don't load the images
options = ChromeOptions()
options.add_argument('lang=en_US') # 设置语言,默认跟浏览器设置一样,答主这里需要英文网页所以改成英文
options.add_experimental_option('prefs',prefs)
service = ChromeService(executable_path="~\chromedriver.exe")
打开一个网页,此处以百度为例:
# open the base url
url = 'https://www.baidu.com/'
driver.get(url)
2 网页元素定位
可以这么理解,网页上所看到的所有的内容都是基于网页元素(element)的精确定义,所以需要先定位到元素,才能去抓取数据,或者做出点击按钮之类的操作。
定位元素
对于结构不是很复杂,而且没有动态加载问题的网页,推荐直接在网页右键,单击检查,找到相应的网页元素的位置,然后右键复制xpath
driver.find_element_xpath('the xpath of the element here')
除了直接去网页结构里面复制xpath,也同要有一些查询功能来查找网页元素
# 通过id(id唯一)
ele = driver.find_element_by_id()
# 通过name,不一定唯一,因为name可以重复
driver.find_element_by_name()
# 通过class_name,不一定唯一,只返回匹配到的第一个元素
driver.find_element_by_class_name()
# 通过tag_name,不是唯一
driver.find_element_by_tag_name()
# 通过元素的超链接文本,不是唯一
driver.find_element_by_link_text()
# 通过元素的部分超链接文本,不是唯一
driver.find_element_by_partial_link_text()
3 元素的常用类方法
当选中元素之后,需要对元素进行操作,下面假设选中的元素是ele,对元素ele做出操作
ele = driver.find_element_by_xpath('xpath I just found')
元素方法:
# 获取元素的文本值
ele.text
# 点击元素
ele.click()
# 如果元素是一个输入框,输入内容
ele.send_keys('你想输入的内容')
# 输入完了点回车
ele.send_keys(Keys.ENTER)
# 如果元素是一个输入框,清空输入框
ele.clear()
4 driver的有用的类方法
# 打开网页链接
driver.get(url)
# 关闭当前页面
driver.close()
# 关闭浏览器并且退出驱动程序
driver.quit()
# 上一页
driver.back()
# 截图
driver.get_screenshot_as_file(filename)
# 窗口最大化
driver.maximize_window()
# 打开一个新标签页
newwindow = 'window.open("新标签页的网址")'
driver.execute_script(newwindow)
# 当有不同标签页时,需要转换到对应标签页才能继续操作,否则还在原页面
driver.switch_to_window(driver.window_handles[1])
之后有时间的话会写一个例子来应用以上所有功能。
相关参考:
【python+selenium的web自动化】- 8种元素定位方式详解https://link.zhihu.com/?target=https%3A//www.cnblogs.com/miki-peng/p/14494485.html
Python Selenium 常用方法总结https://link.zhihu.com/?target=https%3A//blog.51cto.com/u_15127576/4236457