zoukankan      html  css  js  c++  java
  • 了解使用Pyppeteer

    1.Pyppeteer的来源

    如果大家对 Python 爬虫有所了解的话,想必你应该听说过 Selenium 这个库,这实际上是一个自动化测试工具.但是其环境配置的麻烦就不用我多说了.

     Pyppeteer,不是 Puppeteer。它是 Puppeteer 的 Python 版本的实现,但他不是 Google 开发的,是一位来自于日本的工程师依据 Puppeteer 的一些功能开发出来的非官方版本。

    在 Pyppetter 中,实际上它背后也是有一个类似 Chrome 浏览器的 Chromium 浏览器在执行一些动作进行网页渲染.

    2.Pyppeteer的几种基本用法

    pyppeteer github 地址:https://github.com/miyakogi/pyppeteer

    2.1设置网页格式

    driver = await launch({
            # 谷歌浏览器的安装路径
            'executablePath': 'C:Program FilesGoogleChromeApplicationchrome.exe',
            # Pyppeteer 默认使用的是无头浏览器
            'headless': False,
            # 设置Windows-size和Viewport大小来实现网页完整显示
            'args': ['--no-sandbox', '--window-size=1024,768']
        })
    code1

    2.2打开你所需要的网站

    page = await driver.newPage()
    await page.goto(url)
    code2

    2.3通过选择器进行操作

    await page.type(选择器信息,内容,时间)
    #await page.type('#q4','20183769')
    await page.click(选择器信息)
    #await page.click('#divquestion3 > ul > li > a')
    test=await page.querySelector(选择器信息)
    await test.click()
    #date=await page.querySelector('#q1')
    #await date.click()
    code3

    2.4通过xpath 定位/操作

    click_handle = await page.xpath("xpath路径")
    await click_handle[0].click()
    code4

    2.5定位转换frame

    date = await page.querySelector('#q2')
        await date.click()
        frame = page.frames#获取所有的frame
        date2 = await frame[1].querySelector('#selectTodayButton')
        await date2.click()
    code5

    2.6获取所有页面

    page_list = await driver.pages()
    await page_list[-1].content() 
    code6

    3.Pyppeteer几种常用类

    浏览器类

    pyppeteer.browser.Browser()
    这个类是由launch()返回的实例

    • browserContexts: 返回所有打开的浏览器上下文的列表,在新创建的浏览器中,这将返回单个实例
    • coroutine close(): 关闭连接并终止浏览器进程
    • coroutine createIncognitoBrowserContext(): 创建一个新的隐身浏览器上下文。这不会与其他浏览器上下文共享cookie /缓存
    • coroutine disconnect(): 断开浏览器
    • coroutine newPage(): 在此浏览器上创建新页面并返回其对象
    • coroutine pages(): 获取此浏览器的所有页面。返回格式为列表,包含所有页面
    • process:返回此浏览器的进程
    • target(): 获取浏览器中所有活动目标的列表
    • coroutine userAgent():返回浏览器的原始UA
    • coroutine version(): 获取浏览器的版本
    • wsEndpoint: 返回websocket端点url

     键盘类

    • coroutine down(): 如果没有参数,则是按下鼠标左键。如果是键盘的某个值比如shift、A等键,则是相当于按下这些键。
    • coroutine press(): 同down(),但是上面是不会释放鼠标或者键盘,需要调用up方法释放鼠标。
    • coroutine sendCharacter:将字符发送到页面。没用过
    • coroutine type(): 同上
    • coroutine up(): 释放由down按下的键或者鼠标

    worker类

    page.on(‘workercreated’, 函数) 这个函数传入的参数就是worker类

    • coroutine evaluate(): 同上
    • coroutine evaluateHandle():同上
    • coroutine executionContext():同上
    • url: 同上

    Dialog类

    page.on( 'dialog', 函数),函数的参数就是这个类

    • coroutine accept(): 接受对话框
    • defaultValue: 如果对话框提示,则获取默认提示值
    • coroutine dismiss(): 关闭对话框
    • message: 获取对话框消息
    • type: 获取对话框类型。类型有:alertbeforeunloadconfirm,或prompt

    frame

    • coroutine J():
    • coroutine JJ():
    • coroutine JJeval(): 
    • coroutine Jeval(): 
    • coroutine Jx(): 
    • coroutine addScriptTag(): 
    • coroutine addStyleTag(): 
    • childFrames: 获取子框架
    • coroutine click(): 
    • coroutine content(): 
    • coroutine evaluate(): 
    • coroutine evaluateHandle(): 
    • coroutine executionContext(): 
    • coroutine focus(): 
    • coroutine hover(): 
    • isDetached()如果此框架已分离,则返回True
    • name:获取frame的名称,如果没有则返回ID
    • parentFrame:获取父框架
    • 后面这些属性和page类一模一样

    ElementHandle

    • coroutine J()
    • coroutine JJ()
    • coroutine JJeval()
    • coroutine Jeval()
    • asElement
    • coroutine boundingBox(): 返回此元素的边界框,如果元素不可见,则返回None
    • coroutine boxModel():返回元素框
    • coroutine click()
    • coroutine contentFrame(): 返回元素句柄的frame
    • coroutine focus()
    • coroutine hover()
    • coroutine isIntersectingViewport(): 如果元素在视口中可见,则返回True
    • coroutine press()
    • coroutine screenshot()
    • coroutine tap()
    • coroutine type()
    • coroutine uploadFile(): 上传文件

    frame

    • coroutine J():
    • coroutine JJ():
    • coroutine JJeval(): 
    • coroutine Jeval(): 
    • coroutine Jx(): 
    • coroutine addScriptTag(): 
    • coroutine addStyleTag(): 
    • childFrames: 获取子框架
    • coroutine click(): 
    • coroutine content(): 
    • coroutine evaluate(): 
    • coroutine evaluateHandle(): 
    • coroutine executionContext(): 
    • coroutine focus(): 
    • coroutine hover(): 
    • isDetached()如果此框架已分离,则返回True
    • name:获取frame的名称,如果没有则返回ID
    • parentFrame:获取父框架
      后面这些属性和page类一模一样
  • 相关阅读:
    asp.net MVC 3/4 equivalent to a response.filter
    Asp.net MVC Request Life Cycle
    无法完成你的itunes store 请求发生未知错误50
    苹果Mac OS X系统十三年视觉变化发展史
    authentication not supported Connect to TFS Git from Xamarin Studio (non-hosted, locally installed TFS 2013)
    iOS开发者帐号申请指南
    Apple Developer Registration and DUNS Number Not Accepted
    apple developer D-U-N-S® Number
    苹果企业开发者账号申请记录
    D-U-N-S申请流程
  • 原文地址:https://www.cnblogs.com/zzmds/p/14277230.html
Copyright © 2011-2022 走看看