Python Learning Day6
selenium操作
点击、清除操作
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time driver = webdriver.Chrome( ) try:
driver.implicitly_wait(10)
#1、往jd发送请求
driver.get('https://www.jd.com/')
#找到输入框输入围城
input_tag = driver.find_element_by_id('key')
input_tag.send_keys('围城')
#键盘回车
input_tag.send_keys(Keys.ENTER)
time.sleep(2)
#找到输入框输入墨菲定律
input_tag = driver.find_element_by_id('key')
input_tag.clear()
input_tag.send_keys('墨菲定律')
#找到搜索按钮点击搜索
button = driver.find_element_by_class_name('button')
button.click()
time.sleep(10)
finally:
driver.close()
获取cookies
from selenium import webdriver
import time
driver = webdriver.Chrome()
try:
driver.implicitly_wait(10)
driver.get('https://www.zhihu.com/explore')
print(driver.get_cookies())
time.sleep(10)
finally:
driver.close()
选项卡管理:切换选项卡,有js的方式windows.open,有windows快捷键:
import time
from selenium import webdriver
#
browser = webdriver.Chrome()
try:
browser.get('https://www.baidu.com')
#
execute_script: 执行javascrpit代码
#弹窗操作
browser.execute_script('alert("tank")')
#新建浏览器窗口
browser.execute_script(
'''
window.open();
'''
)
time.sleep(1)
print(browser.window_handles) 获取所有的选项卡
#切换到第二个窗口
#新:
browser.switch_to.window(browser.window_handles[1])
#旧:
#browser.switch_to_window(browser.window_handles[1])
# 第二个窗口往淘宝发送请求
browser.get('https://www.taobao.com')
time.sleep(5) #切换到第一个窗口
browser.switch_to_window(browser.window_handles[0])
browser.get('https://www.sina.com.cn') time.sleep(10)
finally:
browser.close()
ActionChangs动作链
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
#
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
try:
driver.switch_to_frame('iframeResult')
#切换到id为iframeResult的窗口内
driver.switch_to.frame('iframeResult')
# 源位置
draggable = driver.find_element_by_id('draggable')
#目标位置
droppable = driver.find_element_by_id('droppable')
#调用ActionChains,必须把驱动对象传进去
#得到一个动作链对象,复制给一个变量
actions = ActionChains(driver)
#方式一: 机器人
#瞬间把源图片位置秒移到目标图片位置
#actions.drag_and_drop(draggable, droppable) 编写一个行为
#actions.perform() 执行编写好的行为
#方式二: 模拟人的行为
source = draggable.location['x']
target = droppable.location['x']
print(source, target)
distance = target - source
print(distance)
#perform:每个动作都要调用perform执行
#点击并摁住源图片
ActionChains(driver).click_and_hold(draggable).perform()
s = 0
while s < distance:
#执行位移操作
ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
s += 2
# 释放动作链
ActionChains(driver).release().perform() time.sleep(10) finally:
driver.close()
前进、后退
from selenium import webdriver
import time
driver = webdriver.Chrome()
try:
driver.implicitly_wait(10)
driver.get('https://www.jd.com/')
driver.get('https://www.baidu.com/')
driver.get('https://www.cnblogs.com/')
time.sleep(2)
#回退操作
driver.back()
time.sleep(1)
#前进操作
driver.forward()
time.sleep(1)
driver.back()
time.sleep(10)
finally:
driver.close()
破解登录
步骤:
1、打开文件的查看,显示隐藏文件
2、找到C:\Users\administortra\AppData\Local\Google\Chrome\User Data
删除Default文件
3、重新打开浏览器,并登陆百度账号
- 此时会创建一个新的Default缓存文件
4、添加cookies
5、关闭谷歌浏览器后执行程序
# 获取options对象,参数对象
options = ChromeOptions() # 获取cookies保存路径
# 'C:\Users\administortra\AppData\Local\Google\Chrome\User Data'
profile_directory = r'--user-data-dir=C:\Users\administortra\AppData\Local\Google\Chrome\User Data' # 添加用户信息目录
options.add_argument(profile_directory)
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import time
# 把参数加载到当前驱动中 chrome_options默认参数,用来接收options对象
driver = webdriver.Chrome(chrome_options=options)
try:
driver.implicitly_wait(10)
driver.get('https://www.baidu.com/')
'''
BDUSS:*****
'''
# 添加用户cookies信息
# name、value必须小写
driver.add_cookie({"name": "BDUSS", "value": "用户session字符串"})
# 刷新操作
driver.refresh()
time.sleep(10)
finally:
driver.close()
爬取京东商品信息
爬取京东商品信息:
请求url:
https://www.jd.com/
提取商品信息:
1.商品详情页
2.商品名称
3.商品价格
4.评价人数
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time def get_good(driver):
try: # 通过JS控制滚轮滑动获取所有商品信息
js_code = '''
window.scrollTo(0,5000);
'''
driver.execute_script(js_code) # 执行js代码 # 等待数据加载
time.sleep(2) # 3、查找所有商品div
# good_div = driver.find_element_by_id('J_goodsList')
good_list = driver.find_elements_by_class_name('gl-item')
n = 1
for good in good_list:
# 根据属性选择器查找
# 商品链接
good_url = good.find_element_by_css_selector(
'.p-img a').get_attribute('href') # 商品名称
good_name = good.find_element_by_css_selector(
'.p-name em').text.replace("\n", "--") # 商品价格
good_price = good.find_element_by_class_name(
'p-price').text.replace("\n", ":") # 评价人数
good_commit = good.find_element_by_class_name(
'p-commit').text.replace("\n", " ") good_content = f'''
商品链接: {good_url}
商品名称: {good_name}
商品价格: {good_price}
评价人数: {good_commit}
\n
'''
print(good_content)
with open('jd.txt', 'a', encoding='utf-8') as f:
f.write(good_content) next_tag = driver.find_element_by_class_name('pn-next')
next_tag.click() time.sleep(2) # 递归调用函数
get_good(driver) time.sleep(10) finally:
driver.close() if __name__ == '__main__': good_name = input('请输入爬取商品信息:').strip() driver = webdriver.Chrome()
driver.implicitly_wait(10)
# 1、往京东主页发送请求
driver.get('https://www.jd.com/') # 2、输入商品名称,并回车搜索
input_tag = driver.find_element_by_id('key')
input_tag.send_keys(good_name)
input_tag.send_keys(Keys.ENTER)
time.sleep(2) get_good(driver)
破解滑动验证
破解极验滑动验证
博客园登录url:
https://account.cnblogs.com/signinreturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F
1、输入用户名与密码,并点击登录
2、弹出滑动验证,获取有缺口与完整的图片
3、通过像素点进行比对,获取滑动位移距离
4、模拟人的行为轨迹
5、开始滑动
from selenium import webdriver # 用来驱动浏览器的
from selenium.webdriver import ActionChains # 破解滑动验证码的时候用的 可以拖动图片
import time
from PIL import Image # pip3 install pillow
import random option = webdriver.ChromeOptions()
option.add_argument('disable-infobars') driver = webdriver.Chrome(chrome_options=option) def get_snap(driver):
# selenium自带的截图网页全屏图片
driver.save_screenshot('snap.png') img = driver.find_element_by_class_name('geetest_canvas_img') left = img.location['x'] upper = img.location['y'] right = left + img.size['width']
lower = upper + img.size['height'] # print(left, upper, right, lower)
img_obj = Image.open('snap.png') # 对屏幕进行截取,获取滑动验证图片
image = img_obj.crop((left, upper, right, lower)) return image def get_image1(driver):
time.sleep(0.2)
js_code = '''
var x = document.getElementsByClassName('geetest_canvas_fullbg')[0].style.display="block";
console.log(x)
''' time.sleep(1)
driver.execute_script(js_code) # 截取图片
img_obj = get_snap(driver) return img_obj def get_image2(driver):
time.sleep(0.2) js_code = '''
var x = document.getElementsByClassName('geetest_canvas_fullbg')[0].style.display="none";
console.log(x)
''' driver.execute_script(js_code) time.sleep(1) # 截取图片
img_obj = get_snap(driver) return img_obj def get_distance(image1, image2):
# 初始值
start = 60 # 滑块色差
color_num = 60 for x in range(start, image1.size[0]):
for y in range(image1.size[1]): rgb1 = image1.load()[x, y] rgb2 = image2.load()[x, y] r = abs(rgb1[0] - rgb2[0])
g = abs(rgb1[1] - rgb2[1])
b = abs(rgb1[2] - rgb2[2]) if not (r < color_num and g < color_num and b < color_num):
return x - 7 def get_stacks(distance):
distance += 20 '''
匀加速\减速运行
v = v0 + a * t 位移:
s = v * t + 0.5 * a * (t**2)
''' # 初速度
v0 = 0 # 加减速度列表
a_list = [3, 4, 5] # 时间
t = 0.2 # 初始位置
s = 0 # 向前滑动轨迹
forward_stacks = [] mid = distance * 3 / 5 while s < distance:
if s < mid:
a = a_list[random.randint(0, 2)] else:
a = -a_list[random.randint(0, 2)] v = v0 stack = v * t + 0.5 * a * (t ** 2) # 每次拿到的位移
stack = round(stack) s += stack v0 = v + a * t forward_stacks.append(stack) back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1] return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks} def main():
try: driver.get('https://passport.cnblogs.com/user/signin')
driver.implicitly_wait(5) # 1.输入用户名与密码,点击登录
username = driver.find_element_by_id('LoginName')
password = driver.find_element_by_id('Password')
login_button = driver.find_element_by_class_name('ladda-label')
time.sleep(1)
username.send_keys('_tank_')
time.sleep(1)
password.send_keys('k46709394.') # 这里需要等待账号密码输入完毕后再点击登录按钮,否则的不弹框
time.sleep(1)
login_button.click()
# time.sleep(3) # 2.点击滑动验证按钮,获取图片
geetest_button = driver.find_element_by_class_name('geetest_slider_button')
geetest_button.click() time.sleep(0.2) # 3.针对完整的图片进行截取
image1 = get_image1(driver) # 4.针对有缺口的图片进行截取
image2 = get_image2(driver) # 5.对比两张图片,获取滑动距离
distance = get_distance(image1, image2) # 6.模拟人为滑动轨迹
stacks = get_stacks(distance) # 7.根据滑动轨迹进行滑动
forward_stacks = stacks['forward_stacks']
back_stacks = stacks['back_stacks'] slider_button = driver.find_element_by_class_name('geetest_slider_button')
time.sleep(0.2) ActionChains(driver).click_and_hold(slider_button).perform() time.sleep(0.2)
for forward_stack in forward_stacks:
ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform()
time.sleep(0.1)
for back_stack in back_stacks:
ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform()
time.sleep(0.1) time.sleep(0.2) ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform()
ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform() ActionChains(driver).release().perform() time.sleep(50) finally:
driver.close() if __name__ == '__main__':
main()
Python Learning Day6的更多相关文章
- python笔记 - day6
python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...
- python learning Exception & Debug.py
''' 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返 ...
- Python Learning Paths
Python Learning Paths Python Expert Python in Action Syntax Python objects Scalar types Operators St ...
- Python Learning
这是自己之前整理的学习Python的资料,分享出来,希望能给别人一点帮助. Learning Plan Python是什么?- 对Python有基本的认识 版本区别 下载 安装 IDE 文件构造 Py ...
- python基础 Day6
python Day6 id 可以获得python的内存地址 id的举例子 a=100 print(id(a)) #140712544153072 这里就是该对象的内存地址 is 判断的是比较内存地址 ...
- How to begin Python learning?
如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...
- python 学习day6(面向对象)
博客部分内容转自:http://www.cnblogs.com/wupeiqi/p/4493506.html 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法 ...
- Experience of Python Learning Week 1
1.The founder of python is Guido van Rossum ,he created it on Christmas in 1989, smriti of ABC langu ...
- Python Learning: 03
An inch is worth a pound of gold, an inch of gold is hard to buy an inch of time. Slice When the sca ...
随机推荐
- body滚动时左侧菜单固定
var top = $(".nav-frame").offset().top $(document).scroll(function(){ if($(this).scrollTop ...
- 非阻塞IO与异步IO
一.非阻塞IO的轮询读写 ---如果当前进程有多个输入终端和多个输出终端呢?while((n=read(STDIN_FILENO,buf,buf_size))>0){ if(write(S ...
- 容器STL
一.迭代器iterator 迭代器是容器的一种遍历方式,每种容器都定义了自己的迭代器类型 声明一个迭代器: 容器名称<数据类型>::iterator 迭代器名称 vector<int ...
- 手动搭建Vue之前奏:搭建webpack项目
手动搭建vue项目 搭建vue前首先搭建webpack项目 首先你应当安装一下npm以及nodejs 安装完成后,进行如下操作: // 创建项目根目录 mkdir some_project_name ...
- InnoDB 和 MyISAM的索引区别
MyISAM索引实现 MyISAM索引文件和数据文件是分离的,索引文件的data域保存记录所在页的地址(物理存储位置),通过这些地址来读取页,进而读取被索引的行数据. MyISAM的索引原理图如下,C ...
- Mozilla Firefox 将获得包含 Tor 模式的扩展组件
据外媒报道,Firefox 浏览器可能很快就会收到一个 Tor 模式的附加组件,它将通过连接 Tor 网络显著增强隐私功能.虽然 Mozilla 和 Tor 团队的最终目标是在浏览器上实现完整功能的 ...
- php+ajax 实现无限树列表
首先介绍我实现的是xhprof插件的日志转为无限树状图,先看效果图: 废话不多说,直接看代码:(辛辛苦苦敲了好久才搞定,逻辑比较多,新手多揣摩) 控制器: 1 <?php 2 3 namespa ...
- 007、Java中定义int型变量
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Web application architecture overview
- NO10 查看Linux操作系统-版本-内核-Linux分区
·看Linux系统: [root@localhost ~]# uname -m (看操作系统)x86_64[root@localhost ~]# uname -a (看操作系统)Linux lo ...