pyppeeter 解决淘宝的滑动验证码

最开始 我做淘宝的滑动验证码 是用的selenium 做的 但是 经常被检测 而且现在大多大型的网站都对selenium进行了反扒限制。

下面这个代码 就是建立 cookie池之后的出现滑块 获取 json数据中的 x5sec 从而达到 暂时避免滑块

import base64
import json
import os
import MySQLdb
import requests
import random
import time
from pyppeteer import launch, connect
import asyncio
from taobaos.IP_agency.change_ip import mians


def inquire_cookie(tao_tid_sums):
   global cookies_cookie
   global cookies_id
   global cookies_x5sec
   cursor_tobao_tfsid.execute('select id,cookie,x5sec from tobao_cookie WHERE id='+ str(tao_tid_sums))
   rows = cursor_tobao_tfsid.fetchone()
   cookies_id = rows[0]
   cookies_cookie = rows[1]
   cookies_x5sec = rows[2]
   print("x5secx5secx5secx5sec", cookies_x5sec)
   print(cookies_id, cookies_cookie)
   cursor_tobao_tfsid.execute("update tobao_cookie set flag='" + "1" + "' where id=" + str(cookies_id))
   conn_tobao_tfsid.commit()
   return cookies_cookie


async def tao_tid(ss, tao_tid_sums):
    global cursor_tobao_tfsid
    global conn_tobao_tfsid
    global cookie1
    conn_tobao_tfsid = MySQLdb.connect(host=host, port=3306, user='root', password='123456', database='tobao_tfsid',
                           charset='utf8')
    cursor_tobao_tfsid = conn_tobao_tfsid.cursor()
    print("#####################获取淘宝的json==name###########################")
    request_url = "https://s.taobao.com/image"
    times = str(time.time())
    timestamp = times.split(".", 1)
    head = {
        "apiVersion": "v1.0.0",
        "appVersion": "v1.0.0",
        "OS": "iOS",
        "OSVersion": "11.4.1",
        "language": "zh",
        "did": "NTIzOTIxNDYwMzMyM2NjZjk1OGM5NjBmYzNlNzg2OTYtZTkxNzg1MzYzNjA4NGM0Mjg4Njg3MmFhNzExMDE1YTgwMDAyLXIwWUtuK0MrS1Y2eDBteWs3WnhDYmQ2ZnovTT0=",
        "reqSeq": "eb144eed4639d36bb6a7b9aa2a563421",
        "timestamp": timestamp[0],
        "userToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJiaXpUeXBlIjoiTE9BTiIsInVzZXJSb2xlIjoyLCJleHAiOjE1NTQ2MTYwMjUsInVzZXJJZCI6MzAxMzkyNDUxNTc3Mzk3MjQ4LCJjaGVja0NvZGUiOiJiZTk1MDEwZjczN2Q0YTU2ODkxNTYyMDBlNDhhZDEyZCJ9.97Km63wUC6IaoSYE1Db8fAxYoW5N-ZQkbiw4kETN2cQ",
       }
    cookies = inquire_cookie(tao_tid_sums)
    if cookies == None:
        cookies ="thw=cn; t=ab4f2d3baee6b253b768b0e61d1a78a6; _m_h5_tk=eabc6992d68d85ab516d16a55f112fd0_1569218599198; _m_h5_tk_enc=69baf8b2950d1796316570188e00b374; hng=CN%7Czh-CN%7CCNY%7C156; enc=og%2B%2FMHqctj6Z6dXS0W4XAqtnOHjOf2y1saojVVX2ATKi8bkV2tLScueFsYdx%2Bnf%2BC5ZIz7Bn47KKF3gjfx4upg%3D%3D; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; cna=xiQPFi/mmyECAXczQBMq1mVU; v=0; cookie2=19ce10ae5d5c6a282bd2a1bd5ac92a2b; _tb_token_=3a5e715b3a15d; alitrackid=www.taobao.com; unb=1867423764; uc3=nk2=2nbe7hmvEnlG5c8h&id2=UondE6M7wE943w%3D%3D&vt3=F8dByuK6VbqRsf8OF2I%3D&lg2=WqG3DMC9VAQiUQ%3D%3D; csg=51045cfe; lgc=%5Cu90ED1392766683; cookie17=UondE6M7wE943w%3D%3D; dnk=%5Cu90ED1392766683; skt=3c0d0db74f61b159; existShop=MTU2OTIyNDkxOQ%3D%3D; tracknick=%5Cu90ED1392766683; _cc_=Vq8l%2BKCLiw%3D%3D; tg=0; _l_g_=Ug%3D%3D; sg=34c; _nk_=%5Cu90ED1392766683; cookie1=BqeBRK46L0JmH0Z%2FeSvIr71mHBfN9O%2FZUqoj9xDqmTU%3D; lastalitrackid=login.taobao.com; uc1=cookie16=Vq8l%2BKCLySLZMFWHxqs8fwqnEw%3D%3D&cookie21=W5iHLLyFeYZ1WM9hVnmS&cookie15=UtASsssmOIJ0bQ%3D%3D&existShop=false&pas=0&cookie14=UoTaEcMD1%2BWTQg%3D%3D&tag=8&lng=zh_CN; mt=ci=14_1; swfstore=109255; whl=-1%260%260%261569224966021; l=cBx7YADRqXIEj2r3BOfwlurza77T3IRf1sPzaNbMiICP_0CH57JCWZC9l1YMCnGVp6CvR37el_8YBeYBqt4RWdav2j-la; isg=BIKCewmLa9URwXcJi9vJ3Ljw04iKsoZt-xgPTsybNPWgHyCZtOBcfUsZz1vGT_4F"
        cookie1 = {
            "cookie": str(cookies) + ";x5sec=" + str(cookies_x5sec)
        }
    else:
        cookie1 = {
            "cookie": str(cookies) + ";x5sec=" + str(cookies_x5sec)
        }
    print("cookie111111111111111111111111111111111", cookie1)
    fl = open(ss, "rb")
    file = {"imgfile": (ss, fl, "image/jpeg")}
    proxies = mians()
    res = requests.post(request_url, proxies=proxies, headers=head, files=file, cookies=cookie1, timeout=20).text
    print(res)
    try:
        jsres = json.loads(res)
    except:print(res)
    else:
        try:
            tp_name = jsres["name"]
            return tp_name
        except:
            tb_url = jsres["url"]
            huakuai_url = "https://s.taobao.com"+str(tb_url)
            await reques_send(huakuai_url)


async def mouse_slide(page=None):
    await asyncio.sleep(1)
    await page.evaluate('window.scrollBy(400,0)')
    try:
        # 鼠标移动到滑块,按下,滑动到头(然后延时处理),松开按键
        await page.hover('#nc_1_n1z')  # 不同场景的验证码模块能名字不同。
    except:return 1, page
    try:
        await page.mouse.down()
        steps = random.randint(58, 80)
        print("steps:{}".format(steps))
        await page.mouse.move(2000, 0, {'steps':steps})
        await page.mouse.up()
    except Exception as e:
        print('{}:验证失败'.format(e))
        return None, page
    else:
        # 判断是否通过
        slider_again = await page.querySelector('.nc_iconfont.icon_warn')
        if slider_again!=None:
            print("验证失败")
            return None, page
        else:
            # await page.screenshot({'path': './headless-slide-result.png'}) # 截图测试
            print('验证通过')
            return 1, page


async def reques_send(url):
    proxies_url = "http://http-dyn.abuyun.com:9020"
    executablePath = r"C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe"
    browser = await launch({'ignoreHTTPSErrors': True, 'headless': False, 'dumpio': True, 'userDataDir': './data', '--enable-automation': False,'executablePath': executablePath,
                            'args': ['--no-sandbox', '--disable-infobars', '–window-size=2500,4877', '--disable-gpu']})
    # 创建页面
    page = await browser.newPage()
    # 设置页面大小
    proxyUser = "HU0UX3L04OEK853D"
    proxyPass = "7252DB6D989819C6"
    # await page.setExtraHTTPHeaders({'Proxy-Authorization': 'Basic ' + ('{"H56R2946P953B99D"}:{"8ADE908B093EFBB9"}').toString('base64')})
    # await page.setExtraHTTPHeaders({'Proxy-Authorization': 'Basic ' + proxyUser + ':' + proxyPass.toString('base64')})
    await page.setExtraHTTPHeaders({'Proxy-Authorization': 'Basic' + base64.urlsafe_b64encode(bytes((proxyUser + ":" + proxyPass), "ascii")).decode("utf8")})
    await page.setViewport({'width': 2500, 'height': 4877})  # 这个是你屏幕大小的宽高
    await page.setUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36')
    await page.goto(url, {'timeout': 1000 * 50})
    await page.evaluate('''() =>{Object.defineProperties(navigator,{webdriver:{get: () => false}})}''')
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }')
    await page.evaluate('''() =>{ window.navigator.chrome = { runtime: {},  }; }''')
    await page.evaluate('''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
    await page.evaluate('''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
    wsEndpoint = browser.wsEndpoint
    # 验证码破解
    jud_i = 0
    try:
        slider = await page.querySelector('#nc_1_n1z')
        if slider:
            print('当前页面出现滑块')
            # await page.screenshot({'path': './headless-login-slide.png'}) # 截图测试
            try:
                flag, page = await mouse_slide(page=page)  # js拉动滑块过去。
            except:
                flag = 1
            while True:
                if jud_i > 5:
                    break
                if flag:
                    print("验证成功")
                    await page.reload()
                    await asyncio.sleep(2)
                    cookie = await page.cookies()
                    print("taobao_tpid++++++++++++++++++++++++++++++++++",cookie)
                    await db_cookie(cookie)  # 获取滑动后的x5
                    break
                    print("================cookiecookie=========================")
                else:
                    jud_i += 1
                    await page.reload()
                    print("再次验证!!")
                    flag, page = await mouse_slide(page=page)
        else:
            print("无验证码")
    except Exception as e:
        print("验证码破解验证码破解验证码破解验证码破解验证码破解验证码破解",e)
    await browser.disconnect()
    browser = await connect({"browserWSEndpoint": wsEndpoint})
    pages = await browser.pages()
    page = pages[-1]
    await browser.close()


async def writecookie(cookie):
    with open('mycookie.json', 'w', encoding='utf8')as f:
        f.write(json.dumps(cookie))

async def db_cookie(cookies):
    cookie = {}
    for i in cookies:
        cookie[i["name"]] = i["value"]
    x5sec_sss = cookie["x5sec"]
    print("获取x5sec保存到数据库", x5sec_sss)
    cursor_tobao_tfsid.execute("update tobao_cookie set x5sec='" + x5sec_sss + "'where id=" + str(cookies_id))
    conn_tobao_tfsid.commit()
    # conn.close()
    await writecookie(cookie)  # 获取cookie 放入文件


async def main():
        ss = "C:/Users/Administrator/Desktop/11.jpg"
        await tao_tid(ss)


if __name__ == '__main__':
    while 1:
        asyncio.get_event_loop().run_until_complete(main())

我还有 淘宝以图搜图的接口 !! 如果有需要 大家可以加我QQ 657589436 互相学习 共同进步

你可能感兴趣的:(Python)