• requests库


    python实现的简单易用的HTTP库。基于urllib,采用Apache2 Licensed开源协议的HTTP库,比urllib更方便,可以节约我们大量的工作,完全满足HTTP测试需求。

     #示例

    import requests
    response = requests.get("https://www.baidu.com")
    print(type(response))
    print(response.status_code)
    print(type(response.text))
    print(response.text)
    print(response.cookies)

    '''各种请求方法'''
    #print('*'*100)
    #requests.post("http://httpbin.org/post")
    #requests.put("http://httpbin.org/put")
    #requests.delete("http://httpbin.org/delete")
    #requests.head("http://httpbin.org/head")
    #requests.options("http://httpbin.org/options")

    '''GET请求'''
    #基本GET请求
    print("*基本GET*"*20)
    response = requests.get("http://httpbin.org/get")
    print(response.text)
    print("*json*"*20)
    print(response.json())
    #带参数的GET请求
    print("*带参数GET*"*20)
    response = requests.get("http://httpbin.org/get?name=zhangsan&age=15")
    print(response.text)
    data = {
    'name':'lisi',
    'age':19
    }
    response = requests.get("http://httpbin.org/get",params=data)
    print(response.text)
    #获取二进制数据
    print("*获取二进制数据*"*20)
    response = requests.get("https://github.com/favicon.ico")
    print(type(response.text),type(response.content))
    print(response.text)
    print(response.content)
    with open("faviconn.ico","wb") as f:
    f.write(response.content)
    f.close()


    #爬虫的时候,如果没有header,服务器会把你禁掉或出现服务器错误等。
    print("*添加header*"*20)
    response = requests.get("https://www.zhihu.com/explore") #直接请求,不加header
    print(response.text)
    headers = {
    'User-Agent':'Mozilla/5.0(Macintosh;Intel Mac OS X 10_11_4)AppleWebKit/537.36(KHTML,like Gecko)Chrome/52.0.2743.116 Safari/537.36'
    }
    response = requests.get("https://www.zhihu.com/explore",headers=headers)
    print(response.text)

    '''POST请求'''
    #基本POST请求
    print("-基本POST请求-"*20)
    data = {"name":"xiaoming","age":"22"}
    response = requests.post("http://httpbin.org/post",data=data)
    print(response.text)
    response = requests.post("http://httpbin.org/post",data=data,headers=headers)
    print(response.json())

    '''状态码判断'''
    print("-状态码判断-"*20)
    response = requests.get("http://httpbin.org")
    exit() if not response.status_code == requests.codes.ok else print("Request Successfully")
    #或者exit() if not response.status_code == 200 else print("Request Successfully")

    '''文件上传'''
    print("-文件上传-"*20)
    files = {"file":open("faviconn.ico","rb")}
    response = requests.post("http://httpbin.org/post",files = files)
    print(response.text)

    '''cookies'''
    print("-cookies-"*20)
    response = requests.get("https://www.baidu.com")
    print(response.cookies)
    for key,value in response.cookies.items():
    print(key + "=" +value)

    '''会话维持'''
    print("-会话维持-"*20)
    requests.get("http://httpbin.org/cookies/set/number/112232")
    response = requests.get("http://httpbin.org/cookies/")
    print(response.text)

    s = requests.Session()
    s.get("http://httpbin.org/cookies/set/number/123456789")
    response = s.get("http://httpbin.org/cookies/")
    print(response.text)

    '''证书验证'''
    print("-证书验证-"*20)
    response = requests.get("https://www.12306.cn")
    print(response.status_code)
    from requests.packages import urllib3
    urllib3.disable_warnings()
    response = requests.get("https://www.12306.cn",verify=False)
    print(response.status_code)
    # response = requests.get("https://www.12306.cn",cert=('/path/server/crt','/path/key'))
    # print(response.status_code)

    '''超时设置、异常处理'''
    print("-超时设置、异常处理-"*20)
    from requests.exceptions import ReadTimeout,ConnectionError,RequestException
    try:
    response = requests.get("http://httpbin.org/get",timeout=0.5)
    print(response.status_code)
    except ReadTimeout:
    print("Timeout")
    except ConnectionError:
    print("Connection error")
    except RequestException:
    print("Error")

    '''认证设置'''
    print("-认证设置-"*20)
    from requests.auth import HTTPBasicAuth
    r = requests.get("http://120.27.34.24:9001",auth=HTTPBasicAuth('user','123'))
    # 或r = requests.get("http://120.27.34.24:9001",auth=('user','123'))
    print(r.status_code)


    ## requests得到的内容乱码问题:
    参考:https://jingyan.baidu.com/article/2c8c281dbb969d0008252a80.html

    html = requests.get("http://********",headers=headers)
    html.encoding = 'utf-8'
    print(html.text)


  • 相关阅读:
    Spring面试题目
    20个非常有用的Java程序片段
    第一个前台页面----xflow的页面
    java中io对文件操作的简单介绍
    java的两种异常runtimeException和checkedException
    jquery的校验规则的方法
    json对象的简单介绍
    http的状态码(中英文)
    eclipse的调试方法的简单介绍
    软件测试人员需要精通的开发语言(3)--- Linux
  • 原文地址:https://www.cnblogs.com/wisir/p/10003792.html
Copyright © 2020-2023  润新知