代码:

import time
import random
import asyncio
import pyppeteer class LoginTaoBao:
"""
类异步
"""
pyppeteer.DEBUG = True
page = None async def _injection_js(self):
"""注入js
"""
await self.page.evaluate('''() =>{ Object.defineProperties(navigator,{
webdriver:{
get: () => false
}
})
}''') async def _init(self):
"""初始化浏览器
"""
browser = await pyppeteer.launch({'headless': False,
'args': [
'--window-size={1300},{600}'
'--disable-extensions',
'--hide-scrollbars',
'--disable-bundled-ppapi-flash',
'--mute-audio',
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
],
'dumpio': True,
})
self.page = await browser.newPage()
# 设置浏览器头部
await self.page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
# 设置浏览器大小
await self.page.setViewport({'width': 1200, 'height': 600}) async def get_cookie(self):
cookies_list = await self.page.cookies()
cookies = ''
for cookie in cookies_list:
str_cookie = '{0}={1};'
str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
cookies += str_cookie
print(cookies)
return cookies async def mouse_slider(self):
"""滑动滑块
"""
await asyncio.sleep(3)
try:
await self.page.hover('#nc_1_n1z')
# 鼠标按下按钮
await self.page.mouse.down()
# 移动鼠标
await self.page.mouse.move(2000, 0, {'steps': 30})
# 松开鼠标
await self.page.mouse.up()
await asyncio.sleep(2)
except Exception as e:
print(e, ' :错误')
return None
else:
await asyncio.sleep(3)
# 获取元素内容
slider_again = await self.page.querySelectorEval('#nc_1__scale_text', 'node => node.textContent')
if slider_again != '验证通过':
return None
else:
print('验证通过')
return True async def main(self, username_, pwd_):
"""登陆
"""
# 初始化浏览器
await self._init()
# 打开淘宝登陆页面
await self.page.goto('https://login.taobao.com')
# 注入js
await self._injection_js()
# 点击密码登陆按钮
await self.page.click('div.login-switch')
time.sleep(random.random() * 2)
# 输入用户名
await self.page.type('#TPL_username_1', username_, {'delay': random.randint(100, 151) - 50})
# 输入密码
await self.page.type('#TPL_password_1', pwd_, {'delay': random.randint(100, 151)})
time.sleep(random.random() * 2)
# 获取滑块元素
slider = await self.page.querySelector('#nc_1__scale_text')
if slider:
print('有滑块')
# 移动滑块
flag = await self.mouse_slider()
if not flag:
print('滑动滑块失败')
return None
time.sleep(random.random() + 1.5)
# 点击登陆
print('点击登陆')
await self.page.click('#J_SubmitStatic')
await asyncio.sleep(100)
else:
print('没滑块')
# 按下回车
await self.page.keyboard.press('Enter') if __name__ == '__main__':
username = input('淘宝用户名')
pwd = input('密码')
login = LoginTaoBao()
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(login.main(username, pwd))
loop.run_until_complete(task)

最新文章

  1. ansible模块lineinfile
  2. (String)151. Reverse Words in a String
  3. [麦先生]如何使用AJAX实现按需加载
  4. Linux_常用命令_02
  5. android SDK 更新
  6. openerp学习笔记 自定义小数精度(小数位数)
  7. Eclipse 创建Maven工程
  8. 徐汉彬:Web系统大规模并发——电商秒杀与抢购
  9. SQL PL基本概念
  10. codeforces567E. President and Roads
  11. TPL异步并行编程之简单使用
  12. C# + Javascript 实现评论回复功能
  13. 笔记:Maven 插件及配置详解
  14. Django 使用mysql 创建项目
  15. SQL查询时,根据日期范围查询周
  16. 如何利用GitHub搜索敏感信息
  17. 关于用IIS在.net平台发布网页的一些坑
  18. 【转】一次SpringMVC+ Mybatis 配置多数据源经历
  19. 尚硅谷springboot学习2-微服务
  20. vs2013安装过程及使用心得

热门文章

  1. mysql 优化2 慢查询
  2. [LC] 45. Jump Game II
  3. 吴裕雄--天生自然操作系统操作笔记:window10显示隐藏文件夹
  4. 署名|单位地址|Abstract
  5. abrupt|promising
  6. 接口测试-chap3-https请求证书问题
  7. Nginx笔记总结十一:Nginx重写规则指南
  8. Data Visualization and D3.js 笔记(1)
  9. ES6中 const 关键字
  10. LaunchImage的设置及对应图片尺寸