zoukankan      html  css  js  c++  java
  • requests-html

    1. requests-html简介

    官方文档:http://html.python-requests.org/

    GiHub项目地址:https://github.com/kennethreitz/requests-html

    使用Python开发的同学一定听说过Requsts库,它是一个用于发送HTTP请求的测试。如比我们用Python做基于HTTP协议的接口测试,那么一定会首选Requsts,因为它即简单又强大。现在作者Kenneth Reitz 又开发了requests-html 用于做爬虫。

    requests-html 是基于现有的框架 PyQuery、Requests、lxml、beautifulsoup4等库进行了二次封装,作者将Requests设计的简单强大的优点带到了该项目中。

    requests-html和其他解析HTML库最大的不同点在于HTML解析库一般都是专用的,所以我们需要用另一个HTTP库先把网页下载下来,然后传给那些HTML解析库。而requests-html自带了这个功能,所以在爬取网页等方面非常方便。

    2. 安装

    pip install requests-html

    3. 使用

    3.1 请求

    from requests_html import HTMLSession
    
    session = HTMLSession()
    
    # 参数
    browser.args = [
    	'--no-sand',
    	'--user-agent=XXXXX'
    ]
    
    # 响应对象 = session.request(......)
    
    # 响应对象 = session.get(......)
    
    # 响应对象 = session.post(......)
    

    参数和requests模块一模一样

    3.2 响应

    r.url
    # 属性和requests模块一毛一样
    

    3.3 解析

    3.3.1 html对象属性

    r.html.absolute_links	# /xx/yy   -->    http://www....../xx/yy
    r.links                 # 路径原样
    r.base_url          	# 网站基础路径
    r.html              	# 解码过的响应内容,相当于requests中的r.text
    r.text
    r.encoding = 'gbk'     # 控制的是r.html.html的解码格式  
    r.raw_html             # 相当于requests中的r.content
    r.pq
    

    3.3.2 html对象方法

    r.html.find('css选择器')             # [element对象, element对象...]
    r.find('css选择器', first = True)     # 对一个element对象
    r.xpath('xpath选择器')
    r.xpath('‘xpath选择器', first = True)
    
    r.search('模板')                  # result对象(匹配第一次)
    # 模板
    r.search('xxx{}yyy{}')[0]
    r.search('xxx{name}yyy{pwd}')['name']
    
    r.search_all('模板')             # 匹配所有, [result对象, result对象,....]
    r.render(.....)               	# 渲染后的结果去替换r.html.html
    
    # 参数
    script:"""
    ( ) => {
    	js代码
    	js代码
    	}
    """ 
    
    scrolldown:n
    sleep:n
    keep_page:True	# True/False
    
    # 绕过网站对webdriver的检测:
    '''
    () =>{
    	Object.defineProperties(navigator,{
    		webdriver:{
    			get: () => undefined
    			}
    		})
    	}
    '''
    

    3.3.3 Element对象方法及属性

    # el为element对象
    el.absolute_links
    el.links
    el.text
    el.html
    el.attrs
    el.find('css选择器')
    el.search('模板')
    el.search_all('模板')
    

    3.3.4 与浏览器交互(r.html.page.XXX)

    async def xxx():
        await r.html.page.XXX
        session.loop.run....(xxx())
        .screenshot({'path': 路径, 'clip': {'x': 1, 'y': 1, 'width': 100, 'height': 100}})
        .evaluate('''() =>{js代码}}''')
        .cookies()
        .type('css选择器','内容',{'delay':100})
        .click('css选择器', {'button': 'left', 'clickCount': 1, 'delay': 0})
        .focus('css选择器')
        .hover('css选择器')
        .waitForSelector('css选择器')
        .waitFor(1000)
    

    3.3.5 键盘事件(r.html.page.keyboard.XXX)

    .down('Shift')
    .up('Shift')
    .press('ArrowLeft')
    .type('喜欢你啊', {'delay': 100})
    

    3.3.6 鼠标事件(r.html.page.mouse.XXX)

    .click(x, y, {
    	'button':'left',
    	'click': 1,
    	'delay': 0
    })
    .down({'button':'left'})
    .up({'button':'left'})
    .move(x,y,{'steps':1})
    
  • 相关阅读:
    莫队总结
    三、模型层(二)
    二、模型层(一)
    五、web杂项
    一. Django入门
    二、js
    一、html和css
    二十一、正则表达式
    END:小练习、涨知识
    二十、协程
  • 原文地址:https://www.cnblogs.com/zj420255586/p/12157327.html
Copyright © 2011-2022 走看看