爬虫请求库——selenium

selenium模块

  selenium 最初是一个自动化测试工具,而爬虫中使用它主要是为了解决 requests 无法直接执行 JavaScript 代码的问题。selenium 的缺点是效率会变得很慢。

  selenium 本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器

from selenium import webdriver

# 主流浏览器
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.PhantomJS()
browser = webdriver.Safari()
browser = webdriver.Edge()

# 其他国产浏览器,例如360
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = r"C:\Users\kevin\AppData\Roaming\360se6\Application\360se.exe" # 这里是360安全浏览器的路径
chrome_options.add_argument(r'--lang=zh-CN') # 这里添加一些启动的参数
browser = webdriver.Chrome(chrome_options=chrome_options)

 

一、安装

安装:

pip3 install selenium

要自动启动浏览器需要安装相应的驱动,selenium3 默认支持的 webdriver 是 Firfox,而 Firefox 需要安装 geckodriver

 

driver 安装

  selenium 需要调用 driver 来控制浏览器,所以要先安装 driver。不同的浏览器使用不同的 driver,并且需要根据浏览器的版本下载对应版本的 driver。

 

火狐浏览器安装 driver

下载链接:https://github.com/mozilla/geckodriver/releases

  • Linux 下载后将 geckodriver 移动到 /usr/local/bin/ 目录下(或者软连接到 /usr/bin/chromdriver)
  • Windows下载后将 geckodriver.exe  加入环境变量,或者将 geckodriver.exe  移动到脚本同级目录

谷歌浏览器安装 driver:

  • windows:下载 chromdriver.exe 放到python安装路径的 scripts 目录中即可,注意最新版本是2.xx,并非2.9
  • Linux:下载 chromdriver.zip,解压缩后将 chromdriver 文件复制到 /usr/bin 文件夹下(或者软连接到 /usr/bin/chromdriver)

  国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/  或者 https://chromedriver.storage.googleapis.com/index.html
  最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads

其他浏览器安装 driver

  其他国产浏览器(如360)的内核大部分都是 Chrome,所以只要下载对应版本的谷歌浏览器的 driver 即可。可能有极个别是 IE 内核,原理也是一样,下载 IE 的 driver,回头再聊。

 

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.page_source
#能自动弹出浏览器并访问百度,就说明成功了
弹出 chrome 浏览器
from selenium import webdriver

brow=webdriver.Firefox()
brow.get('https://www.baidu.com') 
弹出 firefox浏览器
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# 指定360安全浏览器所在路径
chrome_options.binary_location = r"C:\Users\kevin\AppData\Roaming\360se6\Application\360se.exe" 
chrome_options.add_argument(r'--lang=zh-CN') # 这里添加一些启动的参数
browser = webdriver.Chrome(chrome_options=chrome_options)
弹出360急速浏览器
#安装:selenium+phantomjs
pip3 install selenium
下载phantomjs,解压后把phantomjs.exe所在的bin目录放到环境变量
下载链接:http://phantomjs.org/download.html

#验证安装
C:\Users\Administrator>phantomjs
phantomjs> console.log('egon gaga')
egon gaga
undefined
phantomjs> ^C
C:\Users\Administrator>python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver=webdriver.PhantomJS() #无界面浏览器
>>> driver.get('https://www.baidu.com')
>>> driver.page_source
无界面浏览器

 

二、等待元素被加载

  selenium 是自动运行浏览器的一种行为,而浏览器的加载是需要时间的,我们在进行操作之前肯定必须要等到被操作的元素加载出来后才行,所以我们就需要用到等待的操作、

等待的方式分两种

#2、等待的方式分两种:
隐式等待:在browser.get('xxx')前就设置,针对所有元素有效
显式等待:在browser.get('xxx')之后设置,只针对某个元素有效

示例:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素

browser=webdriver.Chrome()#启动浏览器
"""
方式一
隐式等待:在查找所有元素时,如果尚未被加载,则等10秒,在最前面设置
"""
browser.implicitly_wait(10)

browser.get('https://www.baidu.com')


input_tag=browser.find_element_by_id('kw')
input_tag.send_keys('美女')
input_tag.send_keys(Keys.ENTER)



"""
方式二
显式等待:显式地等待某个指定元素被加载
这两条效果与上面一条相同
"""
#wait=WebDriverWait(browser,10)
#wait.until(EC.presence_of_element_located((By.ID,'content_left')))


#查找内容
contents=browser.find_element_by_id('content_left') #没有进行等待操作就直接查找的话,找不到就会报错
print(contents)

browser.close()
View Code

 

三、选择器

  选择器就是用来做对象爬取的内容进行定位用的,选择器有很多,每种的功能都不同

基本选择器

#官网链接:http://selenium-python.readthedocs.io/locating-elements.html
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
import time

driver=webdriver.Chrome()
driver.get('https://www.baidu.com')
wait=WebDriverWait(driver,10)

try:
    #===============所有方法===================
    # 1、find_element_by_id
    # 2、find_element_by_link_text
    # 3、find_element_by_partial_link_text  #模糊匹配
    # 4、find_element_by_tag_name
    # 5、find_element_by_class_name
    # 6、find_element_by_name
    # 7、find_element_by_css_selector
    # 8、find_element_by_xpath  #后续详解
    # 强调:
    # 1、上述均可以改写成find_element(By.ID,'kw')的形式
    # 2、find_elements_by_xxx的形式是查找到多个元素,结果为列表

    #===============示范用法===================
    # 1、find_element_by_id
    print(driver.find_element_by_id('kw'))

    # 2、find_element_by_link_text
    # login=driver.find_element_by_link_text('登录')
    # login.click()

    # 3、find_element_by_partial_link_text
    login=driver.find_elements_by_partial_link_text('')[0]
    login.click()

    # 4、find_element_by_tag_name
    print(driver.find_element_by_tag_name('a'))

    # 5、find_element_by_class_name
    button=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'tang-pass-footerBarULogin')))
    button.click()

    # 6、find_element_by_name
    input_user=wait.until(EC.presence_of_element_located((By.NAME,'userName')))
    input_pwd=wait.until(EC.presence_of_element_located((By.NAME,'password')))
    commit=wait.until(EC.element_to_be_clickable((By.ID,'TANGRAM__PSP_10__submit')))

    input_user.send_keys('18611453110')
    input_pwd.send_keys('lhf@094573')
    commit.click()

    # 7、find_element_by_css_selector
    driver.find_element_by_css_selector('#kw')

    # 8、find_element_by_xpath

    time.sleep(5)

finally:
    driver.close()
基本选择器

xpath

#官网链接:http://selenium-python.readthedocs.io/locating-elements.html
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
import time

driver=webdriver.PhantomJS()
driver.get('https://doc.scrapy.org/en/latest/_static/selectors-sample1.html')
# wait=WebDriverWait(driver,3)
driver.implicitly_wait(3) #使用隐式等待

try:
    # find_element_by_xpath
    #//与/的不同
    # driver.find_element_by_xpath('//body/a')  # 开头的//代表从整篇文档中寻找,body之后的/代表body的儿子,这一行找不到就会报错了

    driver.find_element_by_xpath('//body//a')  # 开头的//代表从整篇文档中寻找,body之后的//代表body的子子孙孙
    driver.find_element_by_css_selector('body a')

    #取第n个
    res1=driver.find_elements_by_xpath('//body//a[1]') #取第一个a标签,这里的索引是从1开始
    print(res1[0].text)

    #按照属性查找,下述三者查找效果一样
    res1=driver.find_element_by_xpath('//a[5]')
    res2=driver.find_element_by_xpath('//a[@href="image5.html"]')
    res3=driver.find_element_by_xpath('//a[contains(@href,"image5")]') #模糊查找
    print('==>', res1.text)
    print('==>',res2.text)
    print('==>',res3.text)


    #其他
    res1=driver.find_element_by_xpath('/html/body/div/a')
    print(res1.text)

    res2=driver.find_element_by_xpath('//a[img/@src="image3_thumb.jpg"]') #找到子标签img的src属性为image3_thumb.jpg的a标签
    print(res2.tag_name,res2.text)

    res3 = driver.find_element_by_xpath("//input[@name='continue'][@type='button']") #查看属性name为continue且属性type为button的input标签
    res4 = driver.find_element_by_xpath("//*[@name='continue'][@type='button']") #查看属性name为continue且属性type为button的所有标签
    
    
    time.sleep(5)

finally:
    driver.close()
xpath

获取标签属性

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素

browser=webdriver.Chrome()

browser.get('https://www.amazon.cn/')

wait=WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,'cc-lm-tcgShowImgContainer')))

tag=browser.find_element(By.CSS_SELECTOR,'#cc-lm-tcgShowImgContainer img')

#获取标签属性,
print(tag.get_attribute('src'))


#获取标签ID,位置,名称,大小(了解)
print(tag.id)
print(tag.location)
print(tag.tag_name)
print(tag.size)


browser.close()
获取标签的属性

 

四、元素交互操作

  交互操作就是指模拟人在网页中进行输入或点击鼠标的操作,主要针对的是 input 框和链接。交互操作中可以自己植入 js 代码

模拟天猫搜索:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
import time

driver=webdriver.Chrome()
driver.get('https://www.tmall.com/')
driver.implicitly_wait(3)

try:
    input_tag=driver.find_element_by_id('mq')#搜索框
    input_tag.send_keys('NIKE')#写入搜索条件
    input_tag.send_keys(Keys.ENTER)#回车

    time.sleep(3)
    input_tag=driver.find_element_by_id('mq')
    input_tag.clear()#清空搜索框
    input_tag.send_keys('科比战靴')
    input_tag.send_keys(Keys.ENTER)


    time.sleep(5)
finally:
    driver.close()
View Code

模拟滑动验证码操作(网页很low逼):

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
import time

driver=webdriver.Chrome()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
#driver.get('http://www.baidu.com')
driver.implicitly_wait(3)

try:
    driver.switch_to.frame('iframeResult')#已被弃用的html标签,作用是html下套html
    driver.switch_to.parent_frame()#返回父级html标签

    source=driver.find_element_by_id('draggable')#源滑块
    target=driver.find_element_by_id('droppable')#目标
    print(source,target)

    #方式一:
    # actions=ActionChains(driver)
    # actions.drag_and_drop(source,target)
    # actions.perform()

    #方式二:
    distance=target.location['x']-source.location['x']#距离
    ActionChains(driver).click_and_hold(source).perform()#模拟按住鼠标不放手

    print(distance)
    s=0
    while s < distance:
        print(s)
        ActionChains(driver).move_by_offset(xoffset=2,yoffset=0).perform()#每次移动2像素
        s+=2
    ActionChains(driver).release().perform()#释放鼠标


    driver.execute_script('alert("xxxxxxxxx")')#可自写js代码,会执行
    time.sleep(6)
finally:
    driver.close()
View Code

补充:frame的切换

#frame相当于一个单独的网页,在父frame里是无法直接查看到子frame的元素的,必须switch_to_frame切到该frame下,才能进一步查找

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素


try:
    browser=webdriver.Chrome()
    browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')

    browser.switch_to.frame('iframeResult') #切换到id为iframeResult的frame

    tag1=browser.find_element_by_id('droppable')
    print(tag1)

    # tag2=browser.find_element_by_id('textareaCode') #报错,在子frame里无法查看到父frame的元素
    browser.switch_to.parent_frame() #切回父frame,就可以查找到了
    tag2=browser.find_element_by_id('textareaCode')
    print(tag2)

finally:
    browser.close()
补充:frame的切换

模拟浏览器的前进后退

#模拟浏览器的前进后退
import time
from selenium import webdriver

browser=webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.get('https://www.taobao.com')
browser.get('http://www.sina.com.cn/')

browser.back()#后腿
time.sleep(10)
browser.forward()#前进
browser.close()
模拟浏览器的前进后退
#cookies
from selenium import webdriver

browser=webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
print(browser.get_cookies())
browser.add_cookie({'k1':'xxx','k2':'yyy'})
print(browser.get_cookies())

# browser.delete_all_cookies()
设置和得到cookies

选项卡管理

#选项卡管理:切换选项卡,有js的方式windows.open,有windows快捷键:ctrl+t等,最通用的就是js的方式
import time
from selenium import webdriver

browser=webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')

print(browser.window_handles) #获取所有的选项卡
browser.switch_to_window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(10)
browser.switch_to_window(browser.window_handles[0])
browser.get('https://www.sina.com.cn')
browser.close()
选项卡管理

异常处理

from selenium import webdriver
from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException

try:
    browser=webdriver.Chrome()
    browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
    browser.switch_to.frame('iframssseResult')

except TimeoutException as e:
    print(e)
except NoSuchFrameException as e:
    print(e)
finally:
    browser.close()
异常处理

 

小项目扩展

#注意:网站都策略都是在不断变化的,精髓在于学习流程。下述代码生效与2017-11-7,不能保证永久有效
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

browser=webdriver.Chrome()

try:
    browser.get('http://mail.163.com/')

    wait=WebDriverWait(browser,5)

    frame=wait.until(EC.presence_of_element_located((By.ID,'x-URS-iframe')))
    browser.switch_to.frame(frame)

    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.m-container')))

    inp_user=browser.find_element_by_name('email')
    inp_pwd=browser.find_element_by_name('password')
    button=browser.find_element_by_id('dologin')
    inp_user.send_keys('18611453110')
    inp_pwd.send_keys('xxxx')
    button.click()

    #如果遇到验证码,可以把下面一小段打开注释
    # import time
    # time.sleep(10)
    # button = browser.find_element_by_id('dologin')
    # button.click()

    wait.until(EC.presence_of_element_located((By.ID,'dvNavTop')))
    write_msg=browser.find_elements_by_css_selector('#dvNavTop li')[1] #获取第二个li标签就是“写信”了
    write_msg.click()


    wait.until(EC.presence_of_element_located((By.CLASS_NAME,'tH0')))
    recv_man=browser.find_element_by_class_name('nui-editableAddr-ipt')
    title=browser.find_element_by_css_selector('.dG0 .nui-ipt-input')
    recv_man.send_keys('378533872@qq.com')
    title.send_keys('圣旨')
    print(title.tag_name)


    frame=wait.until(EC.presence_of_element_located((By.CLASS_NAME,'APP-editor-iframe')))
    browser.switch_to.frame(frame)
    body=browser.find_element(By.CSS_SELECTOR,'body')
    body.send_keys('egon很帅,可以加工资了')

    browser.switch_to.parent_frame() #切回他爹
    send_button=browser.find_element_by_class_name('nui-toolbar-item')
    send_button.click()

    #可以睡时间久一点别让浏览器关掉,看看发送成功没有
    import time
    time.sleep(10000)

except Exception as e:
    print(e)
finally:
    browser.close()
自动登录163邮箱并发送邮件
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
import time


def get_goods(driver):
    try:
        goods=driver.find_elements_by_class_name('gl-item')

        for good in goods:
            detail_url=good.find_element_by_tag_name('a').get_attribute('href')

            p_name=good.find_element_by_css_selector('.p-name em').text.replace('\n','')
            price=good.find_element_by_css_selector('.p-price i').text
            p_commit=good.find_element_by_css_selector('.p-commit a').text

            msg = '''
            商品 : %s
            链接 : %s
            价钱 :%s
            评论 :%s
            ''' % (p_name,detail_url,price,p_commit)

            print(msg,end='\n\n')


        button=driver.find_element_by_partial_link_text('下一页')
        button.click()
        time.sleep(1)
        get_goods(driver)
    except Exception:
        pass

def spider(url,keyword):
    driver = webdriver.Chrome()
    driver.get(url)
    driver.implicitly_wait(3)  # 使用隐式等待
    try:
        input_tag=driver.find_element_by_id('key')
        input_tag.send_keys(keyword)
        input_tag.send_keys(Keys.ENTER)
        get_goods(driver)
    finally:
        driver.close()


if __name__ == '__main__':
    spider('https://www.jd.com/',keyword='科比战靴')
自动爬取京东商品
#首先要安装Pillow       pip3 install pillow
#Pillow:基于PIL,处理python 3.x的图形图像库.因为PIL只能处理到python 2.x,而这个模块能处理Python3.x,目前用它做图形的很多.


###########思路整理##########

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
from PIL import Image #pip3 install pillow

import time

def get_snap(driver):
    driver.save_screenshot('snap.png')#截图
    snap_obj=Image.open('snap.png')#保存
    return snap_obj

def get_image(driver):
    img=driver.find_element_by_class_name('geetest_canvas_img')
    time.sleep(2) #等待图片加载完毕
    size=img.size
    location=img.location
    #获取图片位置
    left=location['x']
    top=location['y']
    right=left+size['width']
    bottom=top+size['height']

    snap_obj=get_snap(driver)
    image_obj=snap_obj.crop((left,top,right,bottom))#截图操作
    # image_obj.show()
    return image_obj

def get_distance(image1,image2):
    start_x=58#滑块最左侧
    threhold=60#去除伪影响
    # print(image1.size)
    # print(image2.size)
    for x in range(start_x,image1.size[0]):
        for y in range(image1.size[1]):
            rgb1=image1.load()[x,y]
            rgb2=image2.load()[x,y]
            res1=abs(rgb1[0]-rgb2[0])
            res2=abs(rgb1[1]-rgb2[1])
            res3=abs(rgb1[2]-rgb2[2])
            if not (res1 < threhold and res2 < threhold and res3 < threhold):
                return x-7#误差范围

def get_tracks(distance):
    distance+=20#故意划过头20像素
    #v=v0+a*t
    #s=v*t+0.5*a*(t**2)

    v0=0
    s=0
    t=0.2
    mid=distance*3/5
    forward_tracks=[]

    while s < distance:
        if s < mid:
            a=2
        else:
            a=-3

        v=v0
        track=v*t+0.5*a*(t**2)
        track=round(track)#取整数
        v0=v+a*t
        s+=track
        forward_tracks.append(track)
    back_tracks=[-1,-1,-1,-2,-2,-2,-3,-3,-2,-2,-1] #20
    return {"forward_tracks":forward_tracks,'back_tracks':back_tracks}

try:
    driver = webdriver.Chrome()#谷歌浏览器
    driver.get('https://passport.cnblogs.com/user/signin')#博客园
    driver.implicitly_wait(10)#隐形等待10秒

    #1、输入账号、密码,然后点击登陆
    input_user=driver.find_element_by_id('input1')
    input_pwd=driver.find_element_by_id('input2')
    login_button=driver.find_element_by_id('signin')

    input_user.send_keys('wall-a')#输入账号
    input_pwd.send_keys('lg19950726..')#输入密码
    login_button.click()#点击登录按钮

    #2、点击验证人机按钮,弹出没有缺口的图
    button=driver.find_element_by_class_name('geetest_radar_tip_content')
    button.click()

    #3、针对没有缺口的图片进行截图
    image1=get_image(driver)

    #4、点击滑动按钮,弹出有缺口的图
    slider_button=driver.find_element_by_class_name('geetest_slider_button')
    slider_button.click()

    #5、针对有缺口的图片进行截图
    image2=get_image(driver)

    #6、对比两张图片,找出缺口,即滑动的位移
    distance=get_distance(image1,image2)
    # print(distance)

    #7、按照人的行为行为习惯,把总位移切成一段段小的位移
    traks_dic=get_tracks(distance)

    #8、按照位移移动
    slider_button=driver.find_element_by_class_name('geetest_slider_button')
    ActionChains(driver).click_and_hold(slider_button).perform()#按住不放手
    #先向前移动
    forward_tracks=traks_dic["forward_tracks"]
    back_tracks=traks_dic["back_tracks"]
    for forward_track in forward_tracks:
        ActionChains(driver).move_by_offset(xoffset=forward_track,yoffset=0).perform()

    #短暂停顿,发现傻逼,移过了
    time.sleep(0.2)

    # 先向后移动
    for back_track in back_tracks:
        ActionChains(driver).move_by_offset(xoffset=back_track,yoffset=0).perform()
    # 抖一抖
    ActionChains(driver).move_by_offset(xoffset=-4,yoffset=0).perform()
    ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform()
    time.sleep(0.1)
    ActionChains(driver).move_by_offset(xoffset=-2,yoffset=0).perform()
    ActionChains(driver).move_by_offset(xoffset=3,yoffset=0).perform()
    time.sleep(0.3)
    ActionChains(driver).release().perform()#松开鼠标


    time.sleep(10)
finally:
    driver.close()






###############优化后的代码(将功能封装成函数调用)#######
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #按照什么方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #键盘按键操作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
from PIL import Image #pip3 install pillow

import time

def get_snap(driver):
    driver.save_screenshot('snap.png')#截图
    snap_obj=Image.open('snap.png')#保存
    return snap_obj

def get_image(driver):
    img=driver.find_element_by_class_name('geetest_canvas_img')
    time.sleep(2) #等待图片加载完毕
    size=img.size
    location=img.location
    #获取图片位置
    left=location['x']
    top=location['y']
    right=left+size['width']
    bottom=top+size['height']

    snap_obj=get_snap(driver)
    image_obj=snap_obj.crop((left,top,right,bottom))#截图操作
    # image_obj.show()
    return image_obj

def get_distance(image1,image2):
    start_x=58#滑块最左侧
    threhold=60#去除伪影响
    # print(image1.size)
    # print(image2.size)
    for x in range(start_x,image1.size[0]):
        for y in range(image1.size[1]):
            rgb1=image1.load()[x,y]
            rgb2=image2.load()[x,y]
            res1=abs(rgb1[0]-rgb2[0])
            res2=abs(rgb1[1]-rgb2[1])
            res3=abs(rgb1[2]-rgb2[2])
            if not (res1 < threhold and res2 < threhold and res3 < threhold):
                return x-7#误差范围

def get_tracks(distance):
    distance+=20#故意划过头20像素
    #v=v0+a*t
    #s=v*t+0.5*a*(t**2)

    v0=0
    s=0
    t=0.2
    mid=distance*3/5
    forward_tracks=[]

    while s < distance:
        if s < mid:
            a=2
        else:
            a=-3

        v=v0
        track=v*t+0.5*a*(t**2)
        track=round(track)#取整数
        v0=v+a*t
        s+=track
        forward_tracks.append(track)
    back_tracks=[-1,-1,-1,-2,-2,-2,-3,-3,-2,-2,-1] #20
    return {"forward_tracks":forward_tracks,'back_tracks':back_tracks}


def crack(driver):#封装滑动的函数
    # 2、点击验证人机按钮,弹出没有缺口的图
    button = driver.find_element_by_class_name('geetest_radar_tip_content')
    button.click()

    # 3、针对没有缺口的图片进行截图
    image1 = get_image(driver)

    # 4、点击滑动按钮,弹出有缺口的图
    slider_button = driver.find_element_by_class_name('geetest_slider_button')
    slider_button.click()

    # 5、针对有缺口的图片进行截图
    image2 = get_image(driver)

    # 6、对比两张图片,找出缺口,即滑动的位移
    distance = get_distance(image1, image2)
    # print(distance)

    # 7、按照人的行为行为习惯,把总位移切成一段段小的位移
    traks_dic = get_tracks(distance)

    # 8、按照位移移动
    slider_button = driver.find_element_by_class_name('geetest_slider_button')
    ActionChains(driver).click_and_hold(slider_button).perform()  # 按住不放手
    # 先向前移动
    forward_tracks = traks_dic["forward_tracks"]
    back_tracks = traks_dic["back_tracks"]
    for forward_track in forward_tracks:
        ActionChains(driver).move_by_offset(xoffset=forward_track, yoffset=0).perform()

    # 短暂停顿,发现傻逼,移过了
    time.sleep(0.2)

    # 先向后移动
    for back_track in back_tracks:
        ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform()
    # 抖一抖
    ActionChains(driver).move_by_offset(xoffset=-4, yoffset=0).perform()
    ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
    time.sleep(0.1)
    ActionChains(driver).move_by_offset(xoffset=-2, yoffset=0).perform()
    ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
    time.sleep(0.3)
    ActionChains(driver).release().perform()  # 松开鼠标



def login_cnblogs(username,pwd):
    driver = webdriver.Chrome()  # 谷歌浏览器driver = webdriver.Chrome()#谷歌浏览器
    try:

        driver.get('https://passport.cnblogs.com/user/signin')#博客园
        driver.implicitly_wait(10)#隐形等待10秒

        #1、输入账号、密码,然后点击登陆
        input_user=driver.find_element_by_id('input1')
        input_pwd=driver.find_element_by_id('input2')
        login_button=driver.find_element_by_id('signin')

        input_user.send_keys(username)#输入账号
        input_pwd.send_keys(pwd)#输入密码
        login_button.click()#点击登录按钮
        # 调用 封装滑动的函数
        crack(driver)

        time.sleep(10)
    finally:
        driver.close()

if __name__ == '__main__':
    login_cnblogs(username='Angelababy',pwd='sonoface')
破解滑动验证码自动登录博客园
#-*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import datetime
import random
from threading import Thread


class Driver:
    def __init__(self, _webdriver, brow_name):
        self.brow = _webdriver
        self.name = brow_name



class OP:
    def __init__(self, drivers ,start_page=1, end_page=11):
        self.start_page = start_page
        self.end_page = end_page
        self.drivers = []
        for driver in drivers:
            self.drivers.append(driver)

    def my_print(self, *args):
        print(datetime.datetime.now(), args)

    def my_sleep(self):
        time.sleep(random.randint(1, 5))

    def main(self, _driver):
        driver = _driver.brow() # 弹出浏览器
        for i in range(self.start_page, self.end_page):
            if _driver.name == 'Safari':
                for j in range(0, 22 if i == 1 else 20):
                    # driver.implicitly_wait(10)
                    url = 'https://www.cnblogs.com/zhuminghui/default.html?page=' + str(i)
                    self.my_print(url)
                    driver.get(url)
                    self.inner(driver, i, j, _driver.name)
                    self.my_sleep()
            else:
                url = 'https://www.cnblogs.com/zhuminghui/default.html?page=' + str(i)
                self.my_print(url)
                driver.get(url)

                for j in range(0,22 if i == 1 else 20):
                    self.inner(driver, i, j, _driver.name)

                    # wait = WebDriverWait(driver, 5)
                    # wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#homepage1_HomePageDays_ctl00_ImageLink')))
                    self.my_sleep()
                    # readmore_list = driver.find_elements_by_class_name('c_b_p_desc_readmore')

        driver.close()

    def inner(self, driver, i, j, name):
        try:
            postTitle2 = driver.find_elements_by_class_name('postTitle2')
            if len(postTitle2) < j + 1:
                self.my_print(name, j + 1, 'end')
                return

            if 'adowsocks' in postTitle2[j].text or '****' in postTitle2[j].text:
                self.my_print('---<<< %s >>> page %s ,line %s  <%s> ---' % (name, i, j + 1, postTitle2[j].text))
                return

            postTitle2[j].click()
            self.my_sleep()
            wait = WebDriverWait(driver, 5)
            wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#blog_post_info_block')))

            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id('blog_post_info_block'))
            title = driver.find_element_by_class_name('postTitle2')
            self.my_print('<<< %s >>>page %s ,line %s  <%s>' % (name, i, j + 1, title.text))
            self.my_sleep()
            driver.back()
            return True
        except Exception as e:
            self.my_print(name, 'Error:', e)
            driver.back()
            self.my_sleep()

    def start(self):
        ts = []
        for driver in self.drivers:
            ts.append(Thread(target=self.main, args=(driver,)))
        for i in ts:
            i.start()
        for i in ts:
            i.join()


if __name__ == '__main__':
    chrome = Driver(webdriver.Chrome, 'Chrome')
    firefox = Driver(webdriver.Firefox, 'Firefox')
    safari = Driver(webdriver.Safari, 'Safari')

    obj = OP(drivers=[chrome, firefox], start_page=1, end_page=13)
    obj.start()
自动打开竹先森的博客园并从头到尾浏览所有的博客文章

 

 

 

                   

posted @ 2018-01-19 17:37  ''竹先森゜  阅读(3077)  评论(0编辑  收藏  举报