Selenium模块是利用程序直接操控浏览器,就像模拟用户与页面交互一样,你只需把程序设定好就可以让他自己执行一系列操作像打开网页,填写内容,提交表单,下载文件等操作。但其运行时间较慢。这里我们以FireFox浏览器作为我们将要去控制的浏览器,那么我们需要在电脑中提前下载安装好FireFox浏览器,同时要下载一个可以让程序控制它的驱动,驱动下载链接为Releases ·
mozilla/geckodriver · GitHub,可以根据自己电脑的版本来下载对应的驱动。下载好后将其解压到自己指定的文件夹中,selenium模块可以通过在cmd中 'pip install selenium’进行安装。以上准备完成后就可以执行接下来的操作了,我们先简单的试一下: #导入相关模块 from selenium import webdriver from selenium.webdriver.chrome.service import Service #将驱动所在路径放入service中 drive = Service('D:\\软件\\geckodriver.exe') #启动火狐浏览器 browser = webdriver.Firefox(service=drive) #将链接指向https://xueshu.baidu.com/ browser.get('https://xueshu.baidu.com/')
下面我们模拟鼠标点击页面,我们在百度学术的右面可以看到各个功能,接下来我们想进入学术分析这一栏目中,应该如何操作呢?首先我们需要获取到学术分析所在的位置,并转到它所代表的链接,这需要我们知道如何在页面中寻找元素。WebDriver中寻找元素通常使用的是find_elements(by=By.*),其中包括以下几种查找方式: 比如这里我们可以根据(LINK_TEXT)“学术分析”所在的位置找到其链接,进而进行模拟点击的操作,使其跳转到“学术分析”页面:from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.action_chains import ActionChains #导入By from selenium.webdriver.common.by import By drive = Service('D:\\软件\\geckodriver.exe') browser = webdriver.Firefox(service=drive) browser.get('https://xueshu.baidu.com/') link = browser.find_element(by=By.LINK_TEXT,value='学术分析') link.click() 按道理来说,这样就能成功转到学术分析所指的页面,然而现在只停在打开的时候的页面外加一堆看不到的错误提示。在进行短暂的思考过后,发现其刚打开页面的时候首先会出现一个页面提示(如首页图所示),需要先点击一下页面(任意位置即可),才能进行后续操作,那么我们也可以利用程序模拟鼠标随意点击下页面再进行后续操作就可以了呀!:from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.action_chains import ActionChains #导入By from selenium.webdriver.common.by import By drive = Service('D:\\软件\\geckodriver.exe') browser = webdriver.Firefox(service=drive) browser.get('https://xueshu.baidu.com/') #随意点击页面,位置为页面横坐标500,纵坐标100处 ActionChains(browser).move_by_offset(500, 100).click().perform() link = browser.find_element(by=By.LINK_TEXT,value='学术分析') link.click()
我们还可以找到网页中的输入文本部分,通常是<input>或<textarea>元素,通过调用send_keys()方法将文本传进去,再使用submit()方法提交表单,等同于点击表单所在的提交按钮,比如这里我们想搜索有关'medical’的文献,即可找到百度学术检索框的位置:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By drive = Service('D:\\软件\\geckodriver.exe') browser = webdriver.Firefox(service=drive) browser.get('https://xueshu.baidu.com/') ipt = browser.find_element(by=By.ID,value='kw') ipt.send_keys('medical') ipt.submit()
如果我们想要的内容不在光标当前所在的页面位置,我们可以通过滚动到该页面的底部进行获取。这里我们利用的是<html>标签,以及Keys的END变量。<html>标签通常是在HTML文件的开头和结尾处,因此我们利用它来滚动页面:from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys drive = Service('D:\\软件\\geckodriver.exe') browser = webdriver.Firefox(service=drive) browser.get('https://xueshu.baidu.com/') #随意点击页面,位置为页面横坐标500,纵坐标100处 ActionChains(browser).move_by_offset(500, 100).click().perform() roll = browser.find_element(by=By.TAG_NAME,value='html') roll.send_keys(Keys.END)
我们还可以模拟点击浏览器按钮比如前进browser.forward(),后退browser.back(),刷新browser.refresh(),关闭窗口browser.quit():from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By drive = Service('D:\\软件\\geckodriver.exe') browser = webdriver.Firefox(service=drive) browser.get('https://xueshu.baidu.com/') browser.refresh() selenium还能做的远不止这些,比如模拟登录,修改cookie等等以上即为本次全部内容,写文不易,如果您觉得我们的推送对您很有帮助,您可以通过赞赏来给予我们超大鼓励!跪谢~[1]Python编程快速上手:让繁琐工作自动化/(美)斯维加特(Al Sweigart)著;王海鹏译. -- 北京:人民邮电出版社,2016.7[2]Selenium Documentation
— Selenium 4.4 documentationhttps://www.v/selenium/docs/api/py/api.html “统计分析与数据挖掘”是一个分享以原创为主的数据挖掘、统计软件操作、生信分析、机器学习等内容的文章、教程和学习视频的公众号。创建的原目的是为了敦促学生学习相关知识和进行写作训练。如果您喜欢我们的推送内容,欢迎点赞、转发、在看,您的支持将是我们深入学习的动力!
|