pyppeteer对于iframe中的滑块

2020-12-21 11:19:31 浏览数 (1)

代码语言:javascript复制
import asyncio
import time
import numpy, random
import pyppeteer
async def main():
    ip = "xxxxxx"      #代理ip
    port = "xxxx"      #代理端口
    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',
                                          '--disable-infobars'
                                          f'--proxy-server={ip}:{port}'
                                      ],
                                      'dumpio': True
                                      })

    page = await browser.newPage()    #打开一个新页面



    await page.goto('url')   #你要去的页面
    time.sleep(1)
    await page.setUserAgent('5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                          '{ webdriver:{ get: () => undefined } }) }')
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                          '{ appVersion:{ get: () => 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 } }) }')

    frame = page.frames   #获取所有的iframe
    iframe = frame[1]
    await iframe.hover("#nc_1_n1z")   #iframe中的元素聚焦
    await page.mouse.down()
    # time.sleep(1000)

    # 3 滑动
    print(random_linspace(100,800))
    start = 0
    end = 0
    for a in random_linspace(4,800):
        a = int(a)
        print(f'滑动{a}')
        end  =a
        print(end, start)
        await  page.mouse.move(end, start)
        start=end




def random_linspace(num, length):

    start, end = 0.5 * (length / num), 1.5 * (length / num)
    # 借助三方库生成一个标准的等差数列,主要是得出标准等差 space
    origin_list = numpy.linspace(start, end, num)
    space = origin_list[2] - origin_list[1]
    # 在标准等差的基础上,设置上下浮动的大小,(上下浮动10%)
    min_random, max_random = -(space / 10), space / 10
    result = []
    # 等差数列的初始值不变,就是我们设置的start
    value = start
    # 将等差数列添加到 list
    result.append(value)
    # 初始值已经添加,循环的次数 减一
    for i in range(num - 1):
        # 浮动的等差值 space
        random_space = space   random.uniform(min_random, max_random)
        value  = random_space
        result.append(value)
    return result



asyncio.get_event_loop().run_until_complete(main())

0 人点赞