zoukankan      html  css  js  c++  java
  • request_html模块(上)

    request_html模块(上)

    牛逼的requests-html库

    安装:

    pip install request-html
    

    请求数据:

    from request_html import HTMLSession
    session = HTMLSession
    
    url = 'https://www.baidu.com/'
    
    # get:
    r = session.get(url=url)
    # post:
    r = session.post(url=url)
    # request
    r = session.request(method='get'/'post', url=url)
    

    HTML对象属性:

    from requests_html import HTMLSession
    session = HTMLSession()
    
    url = 'https://www.zhihu.com/'
    response = session.get(url=url)
    

    response的属性

    print(response)
    print(type(response))
    
    # <Response [200]>
    # <class 'requests_html.HTMLResponse'>
    
    print(dir(response))
    #['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_from_response', '_html', '_next', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'html', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'session', 'status_code', 'text', 'url']
    
    
    响应:
    response.url: 当前路径
        
    response.text: 文本
    
    response.encoding = 'gbk':编码
    
    response.content: 二进制的响应内容
    
    response.json ==>json.loads(r.text)
    
    response.status_code: 返回状态码
    
    response.headers: 返回响应头
    
    response.cookies: 返回cookies
    
    response.history: 返回响应历史
    

    .html

    print(response.html)
    # <HTML url='https://www.zhihu.com/signin?next=%2F'>
    In [18]: type(res.html)
    Out[18]: requests_html.HTML
    '''
    reponse 和 response_html模块自己实现的类
    '''
        
        
    print(dir(response.html))
    ['__aiter__', '__anext__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_async_render', '_encoding', '_html', '_lxml', '_make_absolute', '_pq', 'absolute_links', 'add_next_symbol', 'arender', 'base_url', 'default_encoding', 'element', 'encoding', 'find', 'full_text', 'html', 'links', 'lxml', 'next', 'next_symbol', 'page', 'pq', 'raw_html', 'render', 'search', 'search_all', 'session', 'skip_anchors', 'text', 'url', 'xpath']
    
    

    html对象属性

    response.html.absolute_links: 绝对链接
    response.links:相对链接
    response.base_url: 基本路径
    response.html.html: 网页源码
    response.html.text: 网页文本
    response.html.encoding = 'gbk'
    response.html.raw_html:页面的二进制流
    

    html对象方法:

    find:

    参数

    :param selector: css 选择器
    :param clean: 是否去除页面中的<scpript>和<style>标签,默认False
    :param containing:如果指定有值,只返回包含所给文本的Element对象,默认False
    :param first: 是否返回第一个对象,默认False
    :param _encoding: 字符编码
    

    返回结果

    [Element,Element……] 
    当First为True的时候,只返回第一个Element
    
    response.html.search(XXXX{}YYYY)[0] # 搜索一次
    response.html.search(XXXX{name}YYY{pwd}ZZZ)[name] #只搜索一次
    
    seach_all:

    查找所有符合template对象,返回的是result对象组成的list

    Element对象:

    'absolute_links', 'attrs', 'base_url', 'default_encoding', 'element', 'encoding', 'find', 'full_text', 'html', 'lineno', 'links', 'lxml', 'pq', 'raw_html', 'search', 'search_all', 'session', 'skip_anchors', 'tag', 'text', 'url', 'xpath'
    
    text:

    去掉 之后的文本

    full_text:

    没有去掉 之后的文本值

    attrs:

    返回以字典形式Element对象的属性和属性名

    
    
  • 相关阅读:
    Oracle经典教程学习笔记
    SQL server触发器、存储过程操作远程数据库插入数据,解决服务器已存在的问题
    sublime text3编译C/C++系统提示丢失zlib1.dll解决的方法
    上机题目(0基础)- 数据库事务(Java)
    SGU
    iOS
    iOS刷新某个cell时候crash
    nginx+tomcat反复请求
    加密学教程(Cryptography Tuturials)文件夹
    C/C++与Matlab混合编程初探
  • 原文地址:https://www.cnblogs.com/king-home/p/11323757.html
Copyright © 2011-2022 走看看