Selenium
是针对Web
应用的开源自动化测试工具,通过编写模拟用户操作的脚本,它会打开浏览器Web应用进行黑盒测试。可以方便的用于功能测试、兼容性测试、 稳定性测试及并发测试。目前已被主流浏览器厂商广泛支持,同时也是很多其它自动化测试工具(比如,RobotFramework
)的底层核心技术。
前面我们使用爬虫requests
库来爬取数据,但是爬取的数据是目标服务器响应给我们的,跟真正用浏览器访问页面得到的代码是不一致的。比如我们访问一些懒加载的网站,内容通过滚动条滚动或者是点击更多加载出来的,如果通过requests
直接获取该页面地址,是拿不到该数据的,Selenium
帮助我们解决了这个问题。
我么这里不详细解释该模块的用法,通过两个示例来了解一下它的功能作用,具体更详细的用法可以到官网进行学习:http://www.selenium.org.cn/
环境准备
安装库
下载浏览器引擎
我们这里以谷歌浏览器为例,就放出来谷歌浏览器引擎的地址
http://chromedriver.storage.googleapis.com/index.html
同时可以参考chromedriver
与支持chrome
版本的映射关系
https://blog.csdn.net/huilan_same/article/details/51896672
模拟百度搜索
运行以上代码可以看到程序为我们打开了谷歌浏览器,访问百度,并搜索爬虫学习,控制台打印搜索结果页面源码,然后关闭浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from selenium import webdriver import time
brower = webdriver.Chrome(executable_path=r'E:\site\python\爬虫\chromedriver.exe')
brower.get(url='https://www.baidu.com')
search_input = brower.find_element_by_id('kw')
search_input.send_keys('爬虫学习') time.sleep(2)
brower.find_element_by_id('su').click() time.sleep(3)
print(brower.page_source)
brower.quit()
|
模拟豆瓣电影懒加载
豆瓣之前电影是通过滚动条来触发显示更多电影的,今天测试发现改成了“更多电影”按钮来点击触发显示更多。我们这里连续触发了3次加载更多,最后将该页面下载,然后关闭浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| from selenium import webdriver import time
brower = webdriver.Chrome(executable_path=r'E:\site\python\爬虫\chromedriver.exe')
brower.get(url='https://movie.douban.com/tv/')
search_input = brower.find_element_by_class_name('more') time.sleep(3)
search_input.click() time.sleep(3)
search_input.click() time.sleep(3)
search_input.click() time.sleep(3)
with open('doubanm.html', 'w', encoding='utf8') as f: f.write(brower.page_source)
brower.quit()
|
如果牵涉到滚动条触发的,我们可以使用js
代码来实现控制滚动条条滚动,滚动条滚动一屏的高度
1
| brower.execute_script('window.scrollTo(0, document.body.scrollHeight)')
|
以上两个源码获取到你就可以使用xpath
或者bs4
去解析你想要的结果了
下载豆瓣电影不显示浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| from selenium import webdriver from selenium.webdriver.chrome.options import Options import time
chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu')
brower = webdriver.Chrome(executable_path=r'E:\site\python\爬虫\chromedriver.exe', chrome_options=chrome_options)
brower.get(url='https://movie.douban.com/tv/')
search_input = brower.find_element_by_class_name('more') time.sleep(3)
search_input.click() time.sleep(3)
search_input.click() time.sleep(3)
with open('doubanm.html', 'w', encoding='utf8') as f: f.write(brower.page_source)
brower.quit()
|
登录qq
空间
登录qq
空间稍有不同,我么这里使用账号密码登录,页面中的登录表单使用的一个ifame
,如果直接操控指定元素,控制台会报错,打印元素不存在,这就需要我们先切换到该iframe
才可以操控指定元素。另我这里登录没有出现拖动验证码,这个问题后续解决。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| from selenium import webdriver import time
brower = webdriver.Chrome(executable_path=r'E:\site\python\爬虫\chromedriver.exe')
brower.get(url='https://i.qq.com/') time.sleep(2)
brower.switch_to.frame('login_frame')
brower.find_element_by_id("switcher_plogin").click()
brower.find_element_by_id('u').send_keys('你的qq号') brower.find_element_by_id('p').send_keys('你的qq密码') time.sleep(5)
brower.find_element_by_id('login_button').click()
time.sleep(5)
with open('qqi.html', 'w', encoding='utf8') as f: f.write(brower.page_source) time.sleep(5)
brower.quit()
|