Use requests-html library render method

The use of a .render

from requests_html  import HTMLSession

session  =HTMLSession()
response = session.get('https://www.cnblogs.com/guapitomjoy/')

print(response.html.render())
  • Were injected js
  • Sims operation browser

Two .render parameters

1.script(str)

js code execution

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

2.scrolldown(int)

  • Slide the slider
  • And sleep is associated with a long slide
  • Each time a scroll

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

3.retries(int)

The number of times the page failed to load

4.wait(float)

Load wait time (in seconds) of the page, to prevent the timeout (optional)

5.sleep(int)

In the waiting time after the initial page rendering

6.timeout(int or float)

Page load time line

7.keep_page(bool)

If true, allows you to access the page with r.html.page

8.reload(bool)

If false, then the page will not load from the browser, but loaded from memory

Three .r.html.page interact with the browser

1. Basic grammar

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 events

  • keyboard.down ( 'keyboard name'): Press the keyboard does not pop up (with the keyboard a little less down ( 'h') will only be a h instead hhhhhhh ....)
  • keyboard.up ( 'name keyboard'): Lift button
  • keyboard.press ( 'name keyboard'): Press + bounce
  • keyboard.type ( 'character string input content', { 'delay': 100}) delay after delay time of each sub-unit is inputms

3. mouse events

Click on

  • click ( 'css selector', { 'button': 'left', 'clickCount': 1, 'delay': 0})
    • button of the mouse button left, right, or middle,
    • clickCount: Clicks default number is 1
    • delay: Click the delay time, in milliseconds
  • mouse.click(x, y,{ 'button':'left', 'clickCount':1,'delay':0})
    • x, y: muber data type, click on the object on behalf of the coordinates

Point down without lifting

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

Lift the mouse

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

4. Other

wait

  • waitFor ( 'selector method or timeout')
    • Selector: css selector xpath or one is not according to //the beginning
    • Method: This method is time page.waitForFunction () shorthand
    • Timeout: milliseconds

Waiting to load elements

waitForSelector ( 'css selector')

Get x, y coordinates

 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 code execution

evaluate ( 'js tag string format')

Typing

type ( 'css selector', 'content', { 'delay': 100})

Focus

focus ( 'css selector')

move to

hover ( 'css selector')

Get cookies

cookies()

Set the page size

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

Screenshot

screenshot ({ 'path': save a local path, 'clip': { 'x': 1, 'y': 1, 'width': 100, 'height': 100}})

  • x: x coordinate of the picture
  • y: y coordinate of the picture
  • width: Wide Pictures
  • height: high picture

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/12153557.html