python中render的用法_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 根据是不是//开头

超时时间:单位毫秒

等待元素加载

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坐标

width: 图片宽

height:图片高

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值