想必点击进来的道友对爬虫都有一些了解或者对爬虫已经'仰慕已久',因为博主也是近半年时间才开始真正的接触爬虫,之前未能发现其魅力所在,接触之后则是深陷其中,不能自拔!有所了解的道友应该都知道urllib2/requests等,实现爬虫已经是易如反掌.但是,今天主角是一个被测试耽误的爬虫神器——selenium
PS:爬虫中主要用来解决JavaScript渲染问题,本次分享主要是selenium 的API调用,实现数据自动化获取。
selenium的本职工作
当我在爬虫中使用了selenium之后,当我得知selenium的初衷不是为爬虫设计的时候。本道特别去百度了一下相关的信息,如下:
Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
当然,本次分享是通过python脚本编写相关的代码。
selenium的基础使用
话不多说,直接上代码:
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') print(browser.page_source) #browser.page_source是获取网页的全部html browser.close()
以上代码就通过chrome 打开淘宝www.taobao.com,并获取页面源码的过程。
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_first = browser.find_element_by_id('q') input_second = browser.find_element_by_css_selector('#q') input_third = browser.find_element_by_xpath('//*[@id="q"]') print(input_first,input_second,input_third) browser.close()
常用的查找方法
find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector
除了以上的常用的查找方法,也可以是使用通用的方法
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_first = browser.find_element(BY.ID,'q')#第一个参数传找元素的方式(CSS、XPATH、ID) print(input_first) browser.close() # 多个元素,elements多个s input_first = browser.find_elements_by_id('q')
元素交互操作-搜索框传入关键词进行自动搜索
from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://www.taobao.com') input = browser.find_element_by_id('q') #找到搜索框 input.send_keys('iPhone') #传送入关键词 time.sleep(5) input.clear() #清空搜索框 input.send_keys('狗粮') button = browser.find_element_by_class_name('btn-search') #找到搜索按钮 button.click()
执行JavaScript,有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript(敲黑板,爬虫必考题)
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script('alert("To Bottom")') #直接传入需要执行的js代码
爬虫中有一种加密为JavaScript加密,就是通过JavaScript生成一系列的验证字段,例如:token、cookie等等作为反爬机制。如果对JavaScript熟悉的道友,想必这是小菜一碟,可是人外有人,山外有山,难免碰到难啃的骨头。这时,selenium从天而降,头上万丈光芒,就想佛祖下凡......扯远了,回归正题
selenium可以获取当前的cookie信息,也可以添加cookie信息。
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') print(browser.get_cookies()) browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'}) print(browser.get_cookies()) browser.delete_all_cookies() print(browser.get_cookies())
交互动作,驱动浏览器进行动作,模拟拖拽动作,将动作附加到动作链中串行执行。
from selenium import webdriver from selenium.webdriver import ActionChains #引入动作链 browser = webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult')#切换到iframeResult框架 source = browser.find_element_by_css_selector('#draggable')#找到被拖拽对象 target = browser.find_element_by_css_selector('#droppable')#找到目标 actions = ActionChains(browser)#声明actions对象 actions.drag_and_drop(source, target) actions.perform()#执行动作 # 此处可以用于滑动验证码的破解,但有几个难点,感兴趣的道友可以查询资料,深入了解一下获取元素信息,数据提取
#获取属性 from selenium import webdriver from selenium.webdriver import ActionChains browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) logo = browser.find_element_by_id('zh-top-link-logo')#获取网站logo print(logo) print(logo.get_attribute('class')) browser.close() # 获取文本值 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('zu-top-add-question') print(input.text)#input.text文本值 browser.close() # 获取Id,位置,标签名,大小 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('zu-top-add-question') print(input.id)#获取id print(input.location)#获取位置 print(input.tag_name)#获取标签名 print(input.size)#获取大小 browser.close()
如有错误,请指正!