目录

requests使用

requests请求方法

requests响应对象属性


Requests模块是一个用于网络请求的模块,主要用来模拟浏览器发请求。其实类似的模块有很多,比如urllib,urllib2,httplib,httplib2,他们基本都提供相似的功能。但是这些模块都复杂而且差不多过时了,requests模块简单强大高效,使得其在众多网络请求模块中脱引而出。

requests使用

环境安装:pip install requests

使用流程:

  • 指定url
  • 基于requests模块发送请求
  • 获取响应对象中的数据值
  • 持久化存储(不是必须的)
  • 案例:爬取百度首页的数据

    #1. 导包
    import requests
    #2. 指定url
    url = "https://www.baidu.com"
    #3. 使用GET方法发送请求,该方法会返回一个响应对象
    response = requests.get(url=url)
    #4. 获取响应数据
    print(response.status_code)  # 打印状态码
    print(response.url)          # 打印请求url
    print(response.headers)      # 打印响应头头信息
    print(response.text)         #以文本形式打印网页源码
    
    #保存数据
    response.encoding = 'utf-8'  #指定编码格式,不然打开乱码
    text = response.text
    with open('./2.html','w',encoding='utf-8') as f:
        f.write(text)

    解决requests请求的数据中文乱码问题——》requests请求返回内容 中文乱码问题

    requests请求方法

    上面的案例requests发送了一个GET请求方法,除此之外还有其他的请求方法。最常用的就是GET和POST方法。

  • res = requests.get ()
  • res = requests.post ()
  • res = requests.put ()
  • res = requests.delete ()
  • res = requests.head ()
  • res = requests.options ()
  • 且在指定方法发送请求的时候,有时候还需要在请求方法括号中requests.get(url=url, xx = xx)指定一些参数,如下。先了解一下

    方法

    参数名字

    HTTP头部

    headers

    GET参数

    params

    POST参数

    data

    文件

    files

    Cookies

    cookies

    重定向处理

    allow_ redirects = False/True

    超时

    timeout

    证书验证

    verify = False/True

    工作流(延迟下载)

    stream=False/ True

    事件挂钩

    hooks=dict(response=)

    身份验证

    auth=

    代理

    proxies=

    requests响应对象属性

    在上面爬取百度首页时,response = requests.get(url=url)其返回的是一个响应对象,而如果我们想要获取具体的数据比如响应码或者网页源码时,就需要通过指定响应对象的属性进行获取。如response.status_code获取响应码

  • 获取请求url                                   res. url
  • 状态码                                           res. status_code
  • 响应数据(以字符串形式)       res . text 
  • 返回的是一个原生字符串,是bytes类型     res. content
  • 查看服务器响应头                       res. headers
  • 查看cookie                                   res.cookies
  • 来源:山山而川\’

    物联沃分享整理
    物联沃-IOTWORD物联网 » python之requests模块详解

    发表回复