当前位置:   article > 正文

pyppeteer基本用法_pyppeteer iframe

pyppeteer iframe

1, 点击跳转

#  执行js 点击
#  js 代码 
xx = """document.querySelector('a[data-logintype="phonepwd"]').click()"""
self.page.evaluate(xx)
#  直接点击
await self.page.click('div.account-center-submit')
#	获取本页url
middle_redirect_url = self.page.url
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2,重载

await self.page.reload() 
await self.page.waitFor(1000)  #  等待
await self.page.goto(src)  #  重新请求页面 
await self.page.content()  #  获取页面内容 
# 清空输入框
await self.page.evaluate('''document.querySelector('#loginname').value="" ''')  # 清空输入框
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3, 如何获取标签(同js获取方式一样)

//  对于id标签 使用 #
await self.page.click('#fb_login_phonepwd_btnyes')

//  对于 class标签 使用 .
await self.page.hover('div.secsdk-captcha-drag-icon')

//  对于 其它标签
await self.page.click('div[node-type="message_form"] > div > a > span[node-type="submitStates"]')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4,input输入内容

await self.page.type('#fb_login_phonepwd_pwd', username_, {'delay': random.randint(100, 151) - 50})

#  或者
input_user = await self.page.xpath("//input[@name='loginCode']")
await input_user[0].type(username_, {'delay': random.randint(100, 151) - 50})
  • 1
  • 2
  • 3
  • 4
  • 5

5,获取标签内元素

image_src = await self.page.Jeval('div.JDJRV-img-wrap > div.JDJRV-bigimg >img', 'el => el.src')
#   或者 
elements = await self.page.querySelectorAll("div.channels > div.id-list > div.id-item-container")
   for element in elements:
       ''' 内容分为 客户  达人/创作者 '''
       div_content = await(await element.querySelector('div.title')).getProperty('textContent')
       # print(await div_content.jsonValue())
       if '客户' in str(await div_content.jsonValue()):
           await element.click()
           break
      
#  或者
element = await page.querySelector('h1')
title = await self.page.evaluate('(element) => element.textContent', element)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

6, 获取 文本 text

phone = await pageX.querySelectorEval('div.ui-form-text', 'node => node.textContent')
  • 1

7, 键盘的点击

await self.page.keyboard.press("Tab")
await self.page.keyboard.press("Enter")
  • 1
  • 2

8, 获取iframe中的标签

#  <frame name="leftiframe" id="leftiframe" scrolling="auto" marginwidth="0" marginheight="0" src="lefttree.aspx)">
SaveStudyRecord2 = '''document.querySelector('#rightiframe').contentWindow.document.querySelector('#SaveStudyRecord2')'''
await self.page.click(SaveStudyRecord2)

#	还可通过 获取
self.page.frames
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

9, 获取cookie 和 注入cookie

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
     return cookies

async def _injection_cookie(self, Cookies):
    """
    注入旧的cookie 方便登录
    :param Cookies:
    :return:
    """
    pass
    for _cookie in Cookies.split("; "):
        if len(_cookie.split("=")) >= 2:
            k, v = _cookie.split("=", 1)
            await self.page.setCookie({
                'name': k,
                'value': v,
                'domain': '.domain.com',
                'path': '/',
                'expires': 1605319285,
                'size': 70,
                'httpOnly': False,
                'secure': False,
                'session': False
            })
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

10, 根据内容获取标签

await self.page.evaluate('''$("button:contains('客户登录')").click()''') 
  • 1

11, 滑块处理示例(某宝iframe内嵌滑块请结合步骤8)

链接: link.
链接: link. 切换iframe

    async def mouse_slider_A(self):
        
        iframe_slide_move_js = '''
        aaa = document.querySelector('#nc_2_n1z');
        event = document.createEvent('MouseEvents');
        event.initEvent('mousedown', true, false);
        aaa.dispatchEvent(event);
        event = document.createEvent('MouseEvents');
        event.initEvent('mousemove', true, false);
        Object.defineProperty(event,'clientX',{get(){return %s;}})
        aaa.dispatchEvent(event);
        '''
        iframe_slide_up_js = '''
        event.initEvent('mouseup', true, false);
        aaa.dispatchEvent(event);
        '''
        _step = 0
        step_total = 260
        times = 3 # 滑动次数
        for _s in range(times):
            if times == _s+1:
                _step = step_total
            else:
                _step += random.randint(70, 105)
            await self.page.evaluate(iframe_slide_move_js % _step)
            await asyncio.sleep(0.38)
        await asyncio.sleep(0.8)
        await self.page.evaluate(iframe_slide_up_js)

    async def mouse_slider_B(self):
        """滑动滑块
        """
        await asyncio.sleep(3)
        length_ = round(random.uniform(18, 22))*100  #   经测试 影响不大
        _step = round(random.uniform(5, 15))  #   经测试 20不行
        try:
            await self.page.hover('#nc_1_n1z')
            print("^*" * 20)
            # 鼠标按下按钮
            await self.page.mouse.down()
            # 移动鼠标
            # await asyncio.sleep(80)
            await self.page.mouse.move(length_, 0, {'steps': _step})  #  steps 验证 0-15都可以  20不行
            # 松开鼠标
            await self.page.mouse.up()
            await asyncio.sleep(2)
        except Exception as e:
            print(e, '      :错误')
            return None
        else:
            await asyncio.sleep(3)
            # 获取元素内容
            try:
                slider_again = await self.page.querySelectorEval('#nc_1__scale_text', 'node => node.textContent')
                if slider_again != '验证通过':
                    return None
                else:
                    print('验证通过')
                    return True
            except:
                print(" 检验滑块是否通过 报错 ")
                return None

    async def mouse_slider_C(self):
        """ 分段滑动 """
        _sider = '#nc_1_n1t'
        if not await self.page.J(_sider):
            print(" 没有滑块 ")
            return

        basic_length_ = lambda _x: _x+random.randint(0, 30)
        for length_ in [basic_length_(340) for _ in range(3)]:
            el = await self.page.J(_sider)
            box = await el.boundingBox()
            await self.page.hover(_sider)
            await self.page.mouse.down()
            await self.page.mouse.move(box['x'] + length_*random.randint(50, 70)*0.01, box['y'], {'steps': 30})
            await self.page.waitFor(random.randint(300, 700))
            await self.page.mouse.move(box['x'] + length_*random.randint(85, 92)*0.01, box['y'], {'steps': 30})
            await self.page.waitFor(random.randint(300, 700))
            # await self.page.mouse.move(box['x'] + distance + 29, box['y'], {'steps': 30})
            await self.page.mouse.move(box['x'] + length_, box['y'], {'steps': 30})
            await self.page.mouse.up()
            await asyncio.sleep(2)
            if not await self.page.J(_sider):
                break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

12, 将页面调整成H5页面

ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
await self.page.setUserAgent(ua)
await self.page.setViewport({'width': 375, 'height': 812, "isMobile": True}) # isMobile 注意此参数
  • 1
  • 2
  • 3

13, 全屏截图

#  fullPage代表全屏截图 
await self.page.screenshot({'path': f'./img.jpeg', 'type': 'jpeg', 'fullPage': True}) 
  • 1
  • 2

14, 拦截请求

import asyncio
from pyppeteer import launch


# 请求拦截器函数,设置拦截条件并可作修改
async def intercept_request(interceptedRequest):
    if 'xxx' in interceptedRequest.url:
        await interceptedRequest.continue_({"url": "xxx"}) # 修改url为xxx
    else:
      await interceptedRequest.continue_()

# 响应拦截器函数,设置拦截条件并可作修改
async def intercept_response(interceptedResponse):
    print(interceptedResponse.url)
    if 'https://www.baidu.com/' == interceptedResponse.url:
        response = await interceptedResponse.text() # 获得请求的text内容
        print(response)


async def main():
    launch_params = {
        'headless': False,
        # 'userDataDir': './userdata',
        # 'userDataDir': r'C:\Users\{}\AppData\Local\Google\Chrome\User Data'.format(getpass.getuser()),
        'args': [
            '--disable-extensions',
            '--hide-scrollbars',
            '--disable-bundled-ppapi-flash',
            '--mute-audio',
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-gpu',
            # '--disable-infobars'
            '--window-size=1866,768'
            # '--window-size={1300},{800}'
        ],
        'dumpio': True
    }
    # browser = await launch(options={'devtools': True, 'headless': False})
    browser = await launch(options=launch_params)
    page = await browser.newPage()
    # 设置浏览器大小
    await page.setViewport({'width': 1300, 'height': 800})


    await page.setRequestInterception(True)
    page.on('request', lambda req: asyncio.ensure_future(intercept_request(req)))
    # 设置response拦截器
    page.on('response', lambda rep: asyncio.ensure_future(intercept_response(rep)))


    await page.goto('https://www.baidu.com')
    # await page.screenshot({'path': 'baidu.png'})
    await asyncio.sleep(200)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/372321
推荐阅读
  

闽ICP备14008679号