requests-html的js执行功能简单使用

#!/usr/bin/env python
# encoding: utf-8  
import asyncio
from requests_html import HTMLSession
import time
script = """
    () => {
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight,
            deviceScaleFactor: window.devicePixelRatio,
        }
    }
"""
geturl="""
  () =>
   {
 return{ url:document.location.href}
   }
"""
loginjs="""
  () =>
   {
    var email=document.getElementById("email")
    email.value="用户名"
    var pwd=document.getElementById("pass")
    pwd.value="密码"
    var submit=document.getElementById("loginbutton")
    submit.click()
   }
"""
#运行js
session=HTMLSession()
r=session.get("https://www.facebook.com/login.php?login_attempt=1")
#开始调用pyppeteer
dom=r.html
#page1=dom.render()#相当于await page.evaluate

#page=dom.render(script=script)#相当于await page.evaluate

# print(dir(dom)) #获取dom对象所有的属性和方法
# print(dom.__dict__) #获取dom对象所有的属性和值,字典形式
# print(dom.links) #获取所有相对连接
# print(dom.absolute_links) #获取所有连接的完整路径
#dom.session就相当于requests模块的response了
#print(dom.render(script=geturl))
print(dom.render(script=loginjs))
time.sleep(5)
print(dom.session)

  

posted @ 2018-05-31 17:09  公众号python学习开发  阅读(9551)  评论(0编辑  收藏  举报