美文网首页
03 requests库的使用

03 requests库的使用

作者: yungege | 来源:发表于2019-08-05 22:36 被阅读0次

    03 requests库的使用

    一、请求

    1、请求方法

    # 导入requests库
    import requests
    
    # get()方法
    response = requests.get(url='http://httpbin.org/get')
    print(response.content.decode())
    
    # post()方法
    response = requests.post(url='http://httpbin.org/post',data={'user':'gebilaowang','password':'laowangba'})
    print(response.content.decode())
    
    # 其它方法
    response = requests.put(url='http://httpbin.org/put',data={'user':'gebilaowang','password':'laowangba'})
    print(response.content.decode())
    response = requests.delete(url='http://httpbin.org/delete')
    print(response.content.decode())
    response = requests.head(url='http://httpbin.org/get')
    print(response.content.decode())
    response = requests.options(url='http://httpbin.org/get')
    print(response.content.decode())
    

    2、传递URL参数

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://httpbin.org/get'
    params = {'k1':'v1','k2':3}
    
    # 发起请求
    response = requests.get(url=url,params=params)
    
    print(response.content.decode())
    

    3、headers 请求头

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://httpbin.org/get'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    
    # 发起请求
    response = requests.get(url=url,headers=headers)
    
    print(response.content.decode())
    

    4、cookies 验证

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://httpbin.org/cookies'
    cookies = {
        'cookies1':'123456789'
    }
    
    # 发起请求
    response = requests.get(url=url,cookies=cookies)
    
    print(response.content.decode())
    

    5、代理 proxy

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://example.org'
    proxies = {
        'HTTP':'111.77.197.198:9999',
    }
    
    # 发起请求
    response = requests.get(url=url,proxies=proxies)
    
    print(response.content.decode())
    

    6、重定向

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://github.com'
    
    # 发起请求
    response = requests.get(url=url)
    print(response.status_code) # 200
    
    # 发起请求
    response = requests.get(url=url,allow_redirects=False)  # 禁止重定向
    print(response.status_code) # 301
    

    7、禁止证书验证

    # 导入requests库
    import requests
    import urllib3
    
    # 忽略警告
    urllib3.disable_warnings()
    # 设置参数
    url='http://httpbin.org/get'
    
    # 发起请求
    response = requests.get(url=url,verify=False)
    print(response.status_code) # 200
    

    8、设置超时

    # 导入requests库
    import requests
    
    # 设置参数
    url='http://httpbin.org/get'
    
    # 发起请求
    response = requests.get(url=url,timeout=1)
    print(response.status_code) # 200
    

    二、接收响应

    # 导入requests库
    import requests
    
    # 构造url
    url = 'http://www.baidu.com'
    
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    
    # 发起请求
    response = requests.get(url=url,headers=headers)
    
    # 编码格式
    print(response.encoding)
    response.encoding = 'utf-8'
    # 内容
    print(response.text)
    # 二进制内容
    print(response.content)
    # 状态码
    print(response.status_code)
    # headers
    print(response.headers)
    print(type(response.headers))
    # json
    # print(type(response.json()))
    # cookies
    print(response.cookies)
    # url
    print(response.url)
    

    三、Session对象

    # 导入requests库
    import requests
    # 创建Session
    session = requests.Session()
    # 设置整个session的headers
    session.headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    #发起请求
    response1 = session.get('http://httpbin.org/cookies/set/sessioncookies/123456789')
    response2 = session.get('http://httpbin.org/cookies')
    print(response1.text)
    print(response2.text)
    

    相关文章

      网友评论

          本文标题:03 requests库的使用

          本文链接:https://www.haomeiwen.com/subject/vnzvdctx.html