zoukankan      html  css  js  c++  java
  • requests-html库render的使用

    一.render的使用

    from requests_html  import HTMLSession
    
    session  =HTMLSession()
    response = session.get('https://www.cnblogs.com/pythonywy/')
    
    print(response.html.render())
    
    • 进行js注入
    • 模拟人操作浏览器

    二.render的参数

    1.script(str)

    执行的js代码

    语法:response.html.render(script='js代码字符串格式')

    2.scrolldown(int)

    • 滑动滑块

    • 和sleep联用为多久滑动一次

    语法:response.html.render(scrolldown=页面向下滚动的次数)

    3.retries(int)

    加载页面失败的次数

    4.wait(float)

    加载页面的等待时间(秒),防止超时(可选)

    5.sleep(int)

    在页面初次渲染之后的等待时间

    6.timeout(int or float)

    页面加载时间上线

    7.keep_page(bool)

    如果为真,允许你用r.html.page访问页面

    8.reload(bool)

    如果为假,那么页面不会从浏览器中加载,而是从内存中加载

    三.r.html.page与浏览器交互

    1.基本语法

    from requests_html  import HTMLSession
    
    session  =HTMLSession()
    response = session.get('https://www.cnblogs.com/pythonywy/')
    
    print(response.html.render(keep_page=true))
    
    async def run():
        #交互语句
    	await r.html.page.XXX
    
     
    
    try:
        session.loop.run_until_complete(run())
    finally:
        session.close()
    

    2.键盘事件

    • keyboard.down('键盘名称'):按下键盘不弹起(与键盘有点不太down('h')只会出现一个h而不是hhhhhhh....)
    • keyboard.up('键盘名称'):抬起按键
    • keyboard.press('键盘名称'):按下+弹起
    • keyboard.type('输入的字符串内容',{‘delay’:100}) delay为每个子输入后延迟时间单位为ms

    3.鼠标事件

    点击

    • click('css选择器',{ 'button':'left', 'clickCount':1,'delay':0})
      • button为鼠标的按键left, right, or middle,
      • clickCount:点击次数默认次数为1
      • delay:点击延迟时间,单位是毫秒
    • mouse.click(x, y,{ 'button':'left', 'clickCount':1,'delay':0})
      • x,y:muber数据类型,代表点击对象的坐标

    点下去不抬起

    • mouse.down({'button':xxx,clickCount:xxx})

    抬起鼠标

    • mouse.up({'button':xxx,clickCount:xxx})

    4.其他

    等待

    • waitFor('选择器, 方法 或者 超时时间')

      • 选择器: css 选择器或者一个xpath 根据是不是//开头
      • 方法:时候此方法是page.waitForFunction()的简写
      • 超时时间:单位毫秒

    等待元素加载

    waitForSelector('css选择器')

    获取x,y坐标

        mydic =await r.html.page.evaluate('''() =>{ 
            var a = document.querySelector('#kw')   #对象的css选择器
            var b = a.getBoundingClientRect()
            return {'x':b.x,'y':b.y , 'width':b.width , 'height':b.height }
            }''')
    

    执行js代码

    evaluate('js代码字符串格式')

    输入内容

    type('css选择器',’内容‘,{’delay‘:100})

    聚焦

    focus('css选择器')

    移动动到

    hover('css选择器')

    获取cookies

    cookies()

    设置页面大小

    setViewport({'width': 1366, 'height': 768})

    截图

    screenshot({'path':保存本地路径,'clip':{'x':1,'y':1,'width':100,'height':100}})

    • x:图片的x坐标
    • y:图片的y坐标
    • 图片宽
    • height:图片高
  • 相关阅读:
    服务器端渲染SSR的优缺点?
    Vue轻量级富文本编辑器-Vue-Quill-Editor
    浏览器跨域问题
    CSS中 !important 的使用
    如何使用vue-table-with-tree-grid的树形表格组件
    各类程序员学习路线图
    Vue中 el-table大数据量加载,不分页,节省内存的性能优化
    看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)
    vue中class类名根据绑定的值三元表达式或字符串拼接动态添加类名
    close事件 vue_vue 监听浏览器关闭事件
  • 原文地址:https://www.cnblogs.com/pythonywy/p/11694967.html
Copyright © 2011-2022 走看看