前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >requests-html库render的使用

requests-html库render的使用

作者头像
小小咸鱼YwY
发布2020-06-19 15:20:59
3.6K0
发布2020-06-19 15:20:59
举报
文章被收录于专栏:python-爬虫python-爬虫

一.render的使用

代码语言:javascript
复制
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.基本语法

代码语言:javascript
复制
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坐标

代码语言:javascript
复制
    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坐标
  • width: 图片宽
  • height:图片高
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.render的使用
  • 二.render的参数
    • 1.script(str)
      • 2.scrolldown(int)
        • 3.retries(int)
          • 4.wait(float)
            • 5.sleep(int)
              • 6.timeout(int or float)
                • 7.keep_page(bool)
                  • 8.reload(bool)
                  • 三.r.html.page与浏览器交互
                    • 1.基本语法
                      • 2.键盘事件
                        • 3.鼠标事件
                          • 4.其他
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档