zoukankan      html  css  js  c++  java
  • requests 的使用

    requests 的使用

    1. 基本用法

      requests 从名字也应该可以看出来这是一个专门用于发送请求的库,前面说得 urllib 库有很多不方便的地方,比如说处理网页验证和Cookies,需要写Opener和Handler来处理,然而requests库为我们简化了很多,有了它,Cookies、验证、代理等设置都不是事儿了。

    import requests
    
    response = requests.get('https://www.baidu.com')
    # Response对象
    print(type(response))
    # 状态码
    print(response.status_code)
    # 响应体的内容
    print(response.text)
    # str类型
    print(type(response.text))
    # cookies
    print(response.cookies)

    这里调用了get()方法实现与urllib.request.urlopen()相同的操作,可能会觉得差不多,但是更方便之处在于其他的请求类型依然可以直接调用一个方法解决:

    import requests
    r = requests.post('http://httpbin.org/post')
    r = requests.put('http://httpbin.org/put')
    r = requests.delete('http://httpbin.org/delete')
    r = requests.head('http://httpbin.org/get')
    r = requests.options('http://httpbin.org/get')

    除此以外,requests库还有很多强大之处,下面详细介绍吧。

    2. GET请求

      使用urllib.request.urlopen()方法发送请求时,如果参数是以字段的形式保存的,还需要使用解析模块parse.urlencode()去编译;但是这事通过requests库来做的话,实在太简单了:

    import requests
    
    data = {
      'name':'jonas',
      'age':18    
    }
    response = requests.get('http:httpbin.org/get',params=data)
    print(response.text)

    同样的,也可以设置请求头:

    import requests
    
    headers  = {
      'User-Agent':  'Mozilla/5.0(Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36(KHTML,like Gecko)  Chrome/65.0.3325.162 Safari/537.36'
    }
    response = requests.get('http://httpbin.org/get',headers=headers)
    print(response.text)

    3. POST请求

    使用requests实现POST请求也跟GET类似:

    import requests
    
    data = {'name':'jonas','age':18}
    response = requests.post('http://httpbin.org',data=data)
    print(response.text)

    除了基本的数据以外,requests还可以在发送请求时上传文件:

    import requests
    files = {'file':open('favicon.ico','rb')}
    response = requests.post('http://httpbin.org/post',files=files)
    print(response.text)

    4. Cookies

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

    5. 代理设置

      对于某些网站,在测试的时候请求几次都能正常获取内容,但是一旦开始大规模爬取,对于大规模且频繁的请求,网站可能会弹出验证码,或者跳转到登录认证页面,甚至还可能会禁封客户端的IP,导致一段时间无法访问。这种反爬机制可通过代理来避免:

    import requests
    proxies = {
      'http':'http://10.10.1.10:3128',
      'https':'http://10.10.1.10:1080'    
    }
    requests.get('https://www.taobao.com',proxies=proxies)

    6. 超时设置

      在本机网络状况不好或者服务器响应太慢甚至无响应时,我们可能会等待特别久的时间才可能收到响应,甚至到最后收不到响应而报错。为了防止服务器不能及时响应,应该设置一个超时时间,一旦超过这个设定的时间还没有返回响应,就抛出异常。

    import requests
    response = requests.get('http://httpbin.org',timeout=1)
    print(response.status_code)

    通过timeout参数可以设置超时时间,单位是秒。

    7. 身份验证

    import requests
    from requests.auth import HTTPBasicAuth
    
    response = requests.get('http://localhost:3000',auth=HTTPBasicAuth('username','password'))
    print(response.status_code)
  • 相关阅读:
    C# 判断中文字符的8种方法
    C# GridView页脚汇总
    .NET开发人员十大必备下载工具
    参数修饰符ref,out ,params的区别
    C#一行代码登陆QQ居然碰到这么多麻烦(有意思)
    IIS5IIS6IIS7的ASP.net 请求处理过程比较
    以下放在作业里做调度,每天自动备份和自动删除三天前的备份
    ASP.NET行变色,及禁用编辑,删除按钮
    按钮点击连续触发
    Excel文件的读写实现
  • 原文地址:https://www.cnblogs.com/jonas-von/p/9174971.html
Copyright © 2011-2022 走看看