• python模块(requests,logging)


    一、requests

    Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作。

    1、安装模块

    pip3 install requests

    2、使用模块

    (1)无参数实例
    
    >>> import requests
    
    >>> ret = requests.get('https://www.baidu.com')
    >>> ret.url
    'https://www.baidu.com/'
    >>> ret.text
    '<!DOCTYPE html>
    <!--STATUS OK--><html> <head><meta http-equiv=content-type c
    ontent=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>
    <meta content=always name=referrer><link rel=stylesheet type=text/css href=https...
    
    (2)有参数实例
    
    >>> ret = requests.get("http://httpbin.org/get", params=payload)
    >>> ret.url
    'http://httpbin.org/get?key1=value1&key2=value2'
    >>> ret.text
    '{
     "args": {
     "key1": "value1", 
     "key2": "value2"
     }, 
     "headers
    ": {
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "Host
    ": "httpbin.org", 
     "User-Agent": "python-requests/2.12.1"
     }, 
     "origin
    ": "101.254.232.211", 
     "url": "http://httpbin.org/get?key1=value1&key2=value2
    Get请求
    1、基本post实例
    >>> payload = {'key1': 'value1', 'key2': 'value2'}
    >>> ret = requests.post("http://httpbin.org/post", data=payload)
    >>> ret.text
    '{
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {
        "key1": "v
    alue1", 
        "key2": "value2"
      }, 
      "headers": {
        "Accept": "*/*", 
    
      "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "23", 
        "Conte
    nt-Type": "application/x-www-form-urlencoded", 
        "Host": "httpbin.org", 
    
      "User-Agent": "python-requests/2.12.1"
      }, 
      "json": null, 
      "origin": "
    101.254.232.211", 
      "url": "http://httpbin.org/post"
    }
    '
    
    2、发送请求头和数据实例
    
    >>> import requests
    >>> import json
    >>>
    ... url = 'https://api.github.com/some/endpoint'
    >>> payload = {'some': 'data'}
    >>> headers = {'content-type': 'application/json'}
    >>>
    ... ret = requests.post(url, data=json.dumps(payload), headers=headers)
    >>> ret.text
    '{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}'
    >>> ret.cookies
    <RequestsCookieJar[]>
    post请求
    requests.get(url, params=None, **kwargs)
    requests.post(url, data=None, json=None, **kwargs)
    requests.put(url, data=None, **kwargs)
    requests.head(url, **kwargs)
    requests.delete(url, **kwargs)
    requests.patch(url, data=None, **kwargs)
    requests.options(url, **kwargs)
     
    # 以上方法均是在此方法的基础上构建
    requests.request(method, url, **kwargs)
    其他请求

    参考:http://www.cnblogs.com/wupeiqi/articles/5501365.html

         http://cn.python-requests.org/zh_CN/latest/

    二、logging

    http://www.cnblogs.com/wupeiqi/articles/5501365.html

    第十点logging日志模块

  • 相关阅读:
    单元测试、TDD和BDD的差别
    关于程序员的发展方向
    forin为什么不按照顺序遍历对象
    CSS BEM 书写规范
    c#基础知识20问
    单个编译c#中的cs文件
    c#排序算法(待续)
    (转)getElementByID getElementsByName getElementsByTagName用法详解
    安装SQL Server 2005 性能计数器错误的解决方法
    创建和使用c#DLL(摘自MSDN)
  • 原文地址:https://www.cnblogs.com/willpower-chen/p/6484351.html
Copyright © 2020-2023  润新知