模拟登陆淘宝

目录[-]

淘宝--模拟登录

使用pyppeteer模拟登录淘宝,获取cookie。

代码

# -*- coding: utf-8 -*-


import asyncio
from pyppeteer import launch import time from retry import retry # 设置重试次数用的 count = 1 async def main(username, password, url): # 主函数 browser = await launch({'headless': True, 'args': ["--disable-infobars"]}) # headless设置无界面模式 page = await browser.newPage() await page.goto(url) print('注入js') # 以下为插入中间js,将淘宝会为了检测浏览器而调用的js修改其结果。 await page.evaluate('''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => undefined } }) }''') try: await page.click('a.forget-pwd.J_Quick2Static') print('切换到密码登录页面') cookie = await login(page, username, password) return cookie except Exception as e: print('直接进入密码登录页面', e) cookie = await login(page, username, password) return cookie async def login(page, username, password): # 登录动作 time.sleep(1) print('输入账号和密码') await page.type('input#TPL_username_1', username) time.sleep(1) await page.type('input#TPL_password_1', password) time.sleep(1) # 点击搜索按钮 await page.click('button#J_SubmitStatic') time.sleep(2) print('点击登录') # 在while循环里强行查询某元素进行等待 # while not await page.waitForXPath('//li[@id="J_SiteNavLogin"]'): # return None print('登录成功!') ck = await get_cookie(page) await save_cookie(ck) return ck async def get_cookie(page): # 获取登录后cookie cookies_list = await 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 save_cookie(cookies): # 保存到本地 with open(r'./cookies.txt', 'w', encoding='utf-8') as f: f.write(cookies) print('保存成功') @retry(tries=3) def run(): global count print('第%s次尝试请求' % count) username = '' # 输入你的账号 password = '' # 输入你的密码 url = 'https://login.taobao.com/member/login.jhtml?redirectURL=https://www.taobao.com/' # 协程,开启个无限循环的程序流程,把一些函数注册到事件循环上。当满足事件发生的时候,调用相应的协程函数。 loop = asyncio

转载于:https://www.cnblogs.com/knighterrant/p/10853862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值