用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

淘宝selenium监测代码

2019-08-02 作者:云代码会员举报

[python]代码库

import asyncio
import time
from pyppeteer.launcher import launch
import random
import mytool
js1='''
  ()=>{
  Object.defineProperty(navigator, 'webdriver', {
    get: () => false,
  });
  }
'''
js2='''
  () => {
   window.navigator.chrome = {
    runtime: {},
    // etc.
  };
  }
'''
js3='''() => {
  const originalQuery = window.navigator.permissions.query;
  return window.navigator.permissions.query = (parameters) => (
    parameters.name === 'notifications' ?
      Promise.resolve({ state: Notification.permission }) :
      originalQuery(parameters)
  );
  }
'''
js4='''
() => {
  Object.defineProperty(navigator, 'plugins', {
    get: () => [1, 2, 3, 4, 5],
  })
  }
'''
js5='''
() => {
  Object.defineProperty(navigator, 'languages', {
    get: () => ['en-US', 'en'],
  })
  }
'''

 
async def main(username, pwd, url):
    try:
      browser = await launch({
                      'headless': False,
                      'dumpio': True,
                      "ignoreHTTPSErrors":True,
                      'args': [
                          '--disable-extensions',
                          '--hide-scrollbars',
                          '--disable-bundled-ppapi-flash',
                          '--mute-audio',
                          '--no-sandbox',
                          '--disable-setuid-sandbox',
                          '--disable-gpu',
                          # "--proxy-server=http://127.0.0.1:8080"
                      ],
                  })
      pages = await browser.pages()
      page=pages[0]
      # page = await browser.newPage()
      await 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 page.goto(url,timeout=0,waitUntil=["networkidle2"])
      
      await page.evaluate(js1)
      await page.evaluate(js2)
      await page.evaluate(js3)
      await page.evaluate(js4)
      await page.evaluate(js5)
      # await page.evaluate(js6)
      # await asyncio.sleep(2)
      await page.waitFor(2000)
      await page.click(".show-pwd-login-link")


      print("填写密码")
      await page.type('#fm-login-id', username,{'delay':50})
      await page.type('#fm-login-password', pwd,{'delay':30})
      print("填写密码完成")
      # await asyncio.sleep(5)
      await page.waitFor(5000)

      print("开始滑动")
      try:
        await page.hover('#nc_2_n1z')
      except Exception as e:
        print(e)
        print("没有找到元素")
        await page.click("[type=submit]")
      else:
        print("点击成功")
        await page.mouse.down()
        print("开始移动")
        await page.mouse.move(1000,0,{'delay':200})
        print("移动完成")
        await page.mouse.up()
        print("滑动完成")
      # await asyncio.wait([
      #     page.click("[type=submit]"),
      #     page.waitForNavigation(timeout=0,waitUntil=["load","domcontentloaded"])
      #         ])
        await page.click("[type=submit]")
      # await asyncio.sleep(5)
      print("点击完成")
      # await asyncio.sleep(5)
      await page.waitFor(7000)

      cks=await page.cookies()
      cookies={}
      for ck in cks:
          cookies[ck["name"]]=ck["value"]
      if "cookie1" not in cookies:
        print("再次点击")
        # await asyncio.wait([
        #   page.click("[type=submit]"),
        #   page.waitForNavigation(timeout=0,waitUntil=["load","domcontentloaded"])
        #       ])
        await page.click("[type=submit]")
        print("再次点击完成")
        # await asyncio.sleep(5)
        await page.waitFor(7000)

        cks=await page.cookies()
        # document.cookie

        cookies={}
        for ck in cks:
            cookies[ck["name"]]=ck["value"]

      print(cookies)
      if "cookie1" in cookies:
          with open("E:/python/1688.txt","w") as f:
              f.write(str(cookies))
          try:
            await page.close()
          except:
            pass
          try:
            await browser.close()
          except:
            pass
          return True
      else:
          try:
            await page.close()
          except:
            pass
          try:
            await browser.close()
          except:
            pass
          return False
    except Exception as e:
        print(e)
        try:
          await page.close()
        except:
          pass
        try:
          await browser.close()
        except:
          pass
        raise ArithmeticError


if __name__ == '__main__':
    url="https://havanalogin.taobao.com/mini_login.htm?lang=zh_CN&appName=wap1688&appEntrance=h5&isMobile=true&showWelcomePage=true&hideItem=false&bizTraceId=01b8cf82001b4a9687c18bd6e01497ba&returnUrl=https%3A%2F%2Fm.1688.com%2Fpass.html%3Fdone%3Dhttps%253A%252F%252Fm.1688.com%252F%26__wapatid__%3D01b8cf82001b4a9687c18bd6e01497ba%26__wapalf__%3D1%26spm%3Da26g8.m1688.m-home-search.0&spm=a26g8.m1688.m-home-search.0"
    loop = asyncio.get_event_loop()
    num=0
    while True:
      num+=1
      if num>8:
        raise ArithmeticError
      try:
        p=loop.run_until_complete(main(username, pwd, url))
      except Exception as e:
        print(e)
        print("重新尝试")
      else:
        if p:
          break


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。