Selenium的使用

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(在浏览器上点点点)。
安装:
pip3 install selenium
优缺点:
优:无需再自己操作cookie和header
缺:慢
依赖驱动:
Firefox
https://github.com/mozilla/geckodriver/releases
Chrome
http://chromedriver.storage.googleapis.com/index.html
""" from selenium import webdriver # 配置驱动
option = webdriver.ChromeOptions()
driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver', chrome_options=option) # 1. 控制浏览器打开指定页面
driver.get("https://dig.chouti.com/all/hot/recent/1") # 2. 找到登录按钮
btn_login = driver.find_element_by_xpath('//*[@id="login-link-a"]')
# 3. 点击按钮
btn_login.click() # 4. 找到手机标签
input_user = driver.find_element_by_xpath('//*[@id="mobile"]')
# 5. 找到密码标签
input_pwd = driver.find_element_by_xpath('//*[@id="mbpwd"]') # 6. 输入用户名
input_user.send_keys('')
# 7. 输入密码
input_pwd.send_keys('woshiniba') # 8. 点击登录按钮
input_submit = driver.find_element_by_xpath(
'//*[@id="footer-band"]/div[5]/div/div/div[1]/div[2]/div[4]/div[2]/div/span[1]')
input_submit.click() print(driver.get_cookies()) # 9. 点击跳转
news = driver.find_element_by_xpath('//*[@id="newsContent20646261"]/div[1]/a[1]')
# news.click()
driver.execute_script("arguments[0].click();", news) # 10.管理浏览器
driver.close()

路飞学城破解极验

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
import os
import shutil
from PIL import Image
import time def get_snap(driver):
driver.save_screenshot('full_snap.png')
page_snap_obj = Image.open('full_snap.png') return page_snap_obj def get_image(driver):
img = driver.find_element_by_class_name('geetest_canvas_img')
time.sleep(2)
location = img.location
size = img.size left = location['x']
top = location['y']
right = left + size['width']
bottom = top + size['height'] page_snap_obj = get_snap(driver) image_obj = page_snap_obj.crop((left * 2, top * 2, right * 2, bottom * 2))
# image_obj.show()
with open('code.png', 'wb') as f:
image_obj.save(f, format='png')
return image_obj def get_distance(image1, image2):
# start = 0
# threhold = 70
# for i in range(start, image1.size[0]):
# for j in range(0, image1.size[1]):
# rgb1 = image1.load()[i, j]
# rgb2 = image2.load()[i, j]
# res1 = abs(rgb1[0] - rgb2[0])
# res2 = abs(rgb1[1] - rgb2[1])
# res3 = abs(rgb1[2] - rgb2[2])
# # print(res1,res2,res3)
# if not (res1 < threhold and res2 < threhold and res3 < threhold):
# print(111111, i, j)
# return i - 13
# print(2222, i, j)
# return i - 13
start = 0
threhold = 70
v = []
for i in range(start, image1.size[0]):
for j in range(0, image1.size[1]):
rgb1 = image1.load()[i, j]
rgb2 = image2.load()[i, j]
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):
print(i)
if i not in v:
v.append(i) stop = 0
for i in range(0, len(v)):
val = i + v[0]
if v[i] != val:
stop = v[i]
break width = stop - v[0]
print(stop, v[0], width)
return width def get_tracks(distance):
import random
exceed_distance = random.randint(0, 5)
distance += exceed_distance # 先滑过一点,最后再反着滑动回来
v = 0
t = 0.2
forward_tracks = [] current = 0
mid = distance * 3 / 5
while current < distance:
if current < mid:
a = random.randint(1, 3)
else:
a = random.randint(1, 3)
a = -a
s = v * t + 0.5 * a * (t ** 2)
v = v + a * t
current += s
forward_tracks.append(round(s)) # 反着滑动到准确位置
v = 0
t = 0.2
back_tracks = [] current = 0
mid = distance * 4 / 5
while abs(current) < exceed_distance:
if current < mid:
a = random.randint(1, 3)
else:
a = random.randint(-3, -5)
a = -a
s = -v * t - 0.5 * a * (t ** 2)
v = v + a * t
current += s
back_tracks.append(round(s))
return {'forward_tracks': forward_tracks, 'back_tracks': list(reversed(back_tracks))} def crack(driver): # 破解滑动认证
# 1、点击按钮,得到没有缺口的图片
button = driver.find_element_by_xpath('//*[@id="embed-captcha"]/div/div[2]/div[1]/div[3]')
button.click() # 2、获取没有缺口的图片
image1 = get_image(driver) # 3、点击滑动按钮,得到有缺口的图片
button = driver.find_element_by_class_name('geetest_slider_button')
button.click() # 4、获取有缺口的图片
image2 = get_image(driver) # 5、对比两种图片的像素点,找出位移
distance = get_distance(image1, image2)
print(distance)
#
# 6、模拟人的行为习惯,根据总位移得到行为轨迹
tracks = get_tracks(int(distance / 2)) # 7、按照行动轨迹先正向滑动,后反滑动
button = driver.find_element_by_class_name('geetest_slider_button')
ActionChains(driver).click_and_hold(button).perform() # 正常人类总是自信满满地开始正向滑动,自信地表现是疯狂加速
for track in tracks['forward_tracks']:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 结果傻逼了,正常的人类停顿了一下,回过神来发现,卧槽,滑过了,然后开始反向滑动
time.sleep(0.5)
for back_track in tracks['back_tracks']:
ActionChains(driver).move_by_offset(xoffset=back_track, yoffset=0).perform()
#
# # 小范围震荡一下,进一步迷惑极验后台,这一步可以极大地提高成功率
ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform() # # 成功后,骚包人类总喜欢默默地欣赏一下自己拼图的成果,然后恋恋不舍地松开那只脏手
time.sleep(0.5)
ActionChains(driver).release().perform() def login_luffy(username, password):
driver = webdriver.Chrome('/Users/wupeiqi/drivers/chromedriver')
driver.set_window_size(960, 800)
try:
# 1、输入账号密码回车
driver.implicitly_wait(3)
driver.get('https://www.luffycity.com/login') input_username = driver.find_element_by_xpath('//*[@id="router-view"]/div/div/div[2]/div[2]/input[1]')
input_pwd = driver.find_element_by_xpath('//*[@id="router-view"]/div/div/div[2]/div[2]/input[2]') input_username.send_keys(username)
input_pwd.send_keys(password) # 2、破解滑动认证
crack(driver) time.sleep(10) # 睡时间长一点,确定登录成功
finally:
pass
# driver.close() if __name__ == '__main__':
login_luffy(username='wupeiqi', password='')

python爬虫之Selenium的更多相关文章

  1. [Python爬虫]使用Selenium操作浏览器订购火车票

    这个专题主要说的是Python在爬虫方面的应用,包括爬取和处理部分 [Python爬虫]使用Python爬取动态网页-腾讯动漫(Selenium) [Python爬虫]使用Python爬取静态网页-斗 ...

  2. Python 爬虫利器 Selenium 介绍

    Python 爬虫利器 Selenium 介绍 转 https://mp.weixin.qq.com/s/YJGjZkUejEos_yJ1ukp5kw 前面几节,我们学习了用 requests 构造页 ...

  3. Python爬虫之selenium的使用(八)

    Python爬虫之selenium的使用 一.简介 二.安装 三.使用 一.简介 Selenium 是自动化测试工具.它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏 ...

  4. Python爬虫之selenium高级功能

    Python爬虫之selenium高级功能 原文地址 表单操作 元素拖拽 页面切换 弹窗处理 表单操作 表单里面会有文本框.密码框.下拉框.登陆框等. 这些涉及与页面的交互,比如输入.删除.点击等. ...

  5. Python爬虫之selenium库使用详解

    Python爬虫之selenium库使用详解 本章内容如下: 什么是Selenium selenium基本使用 声明浏览器对象 访问页面 查找元素 多个元素查找 元素交互操作 交互动作 执行JavaS ...

  6. python爬虫利器Selenium使用详解

    简介: 用pyhon爬取动态页面时普通的urllib2无法实现,例如下面的京东首页,随着滚动条的下拉会加载新的内容,而urllib2就无法抓取这些内容,此时就需要今天的主角selenium. Sele ...

  7. Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息

    目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...

  8. python爬虫——用selenium爬取京东商品信息

    1.先附上效果图(我偷懒只爬了4页)  2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...

  9. Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

    1,引言 在Python网络爬虫内容提取器一文我们详细讲解了核心部件:可插拔的内容提取器类gsExtractor.本文记录了确定gsExtractor的技术路线过程中所做的编程实验.这是第二部分,第一 ...

随机推荐

  1. iOS开发:解决UIScrollView不滚动的问题

    照着书上的Demo(iOS 5.0的教程),在- (void)viewDidLoad里设置scrollView的contentsize,让它大于屏幕的高度,却发现在模拟器中没用,还是不能滚.经过 一翻 ...

  2. 开发ionic准备之安卓模拟器设置(2)

    发现这个安卓模拟器设置屏幕还不能太大,太大显示不全,然后整个模拟器不能拖动,所以尽量不要设置太大的分辨率 ,如下即可 如果选安卓4.4然后勾选了其他下面的ok还不能点击的话,这下要去sdk manag ...

  3. 设计模式之十八:桥接模式(Bridge)

    桥接模式: 将抽象部分和它的实现部分相分离开来,以使它们能够单独地变化. UML图: 主要包含: Abstraction:定义了抽象部分的接口.操作一个实现部分对象的引用. RefinedAbstra ...

  4. 转载(Asp.net Core 中试使用ZKWeb.System.Drawing)

    完美 原文Link: https://www.yanning.wang/archives/644.html 记录下做备份. 很少用Linux服务器. 这下可给整的够呛, 特别是按照官网竟然还不行, 所 ...

  5. 【SpringMVC学习09】SpringMVC与前台的json数据交互

    json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...

  6. Mac 下Versions的 svn无法上传 .a 文件的问题

    Xcode自带的svn和Versions以及一些其它工具都默认ignore".a"文件. 解决办法有两个: 方法一:使用命令行添加文件([转]原文在这) 1.打开终端,输入cd,空 ...

  7. Machine Learning——Unsupervised Learning(机器学习之非监督学习)

    前面,我们提到了监督学习,在机器学习中,与之对应的是非监督学习.无监督学习的问题是,在未加标签的数据中,试图找到隐藏的结构.因为提供给学习者的实例是未标记的,因此没有错误或报酬信号来评估潜在的解决方案 ...

  8. oracle中的not in和not exists注意事项

    NOT IN:不包括空值 NOT EXISTS:包括空值

  9. MongoDB入门学习(1)

    什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...

  10. 《TomCat与Java Web开发技术详解》(第二版) 第六章节的学习总结 ---- JSP技术

    第六章主要介绍了JSP的相关知识. 1.JSP:是通过在HTML文件中加入java程序片段(Java Scriptlet)和JSP标记,就构成了JSP文件.JSP实质上是Servlet.JSP的API ...