• Python requests各种请求接口


    requests模块(请求接口)

     

    下面分别是get,post,入参json,添加cookie,添加header,上传/下载文件 的接口请求举例:

    import requests   #导入模块

    #1.发get请求

    url = 'http://api.xx**.cn/api/xx/stu_info'

    data = {'stu_name':'xx'}  #请求数据

    req = requests.get(url,params=data)   #发get请求

    print(req.json())  #返回字典类型

    print(req.text())  #返回string,json串

    #2.发post请求

    url = 'http://api.xx**.cn/api/xx/login'

    data = {'username':'lyl','passwd':'23456'} #请求数据

    req = requests.post(url,data)  #发post请求

    print(req.json())  #返回json串

    #3.入参是json类型的

    import random
    # phone=random.randint(10000000000,99999999999)
    url='http://api.xx**.cn/api/xx/add_stu'
    data = {
      "name":"小1",
      "grade":"天蝎座",
      "phone":phone,
      "sex":"男",
      "age":28,
      "addr":"河南省济源市北海大道32号"
      }
    req = requests.post(url,json=data)   #请求数据
    print(req.json())

    #4、添加cookie
    url = 'http://api.xx**.cn/api/xxx/gold_add'
    data = {'stu_id':468,'gold':10000}
    djl = {'fancy':'337ca4cc825302b3a8791ac7f9dc4bc6'}
    req = requests.post(url,data,cookies=djl)
    print(req.json())

    #5.添加header

    url = 'http://api.xx**.cn/api/xxx/all_stu'
    header = {
    'Referer':'http://api.xx**.cn/'
     }
    req = requests.get(url,headers=header)
    print(req.json())


    #6、上传文件
    url= 'http://api.xx**.cn/api/file/file_upload'
    data = {
    'file':open(r'C:UsersxxddengDesktop图6bd9026dt935575932465&690.jpg','rb')
     }
    req= requests.post(url,files=data)
    print(req.json())


    #7、下载文件
    url = 'http://up.mcyt.net/?down/46779.mp3'
    req = requests.get(url)
    fw = open('aaa.mp3','wb')
    fw.write(req.content)

    python爬虫使用requests发送post请求示例详解

     更新时间:2020年08月05日 16:51:08   作者:小熊&Gary  
     
    这篇文章主要介绍了python爬虫使用requests发送post请求示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
     

    简介

    HTTP协议规定post提交的数据必须放在消息主体中,但是协议并没有规定必须使用什么编码方式。服务端通过是根据请求头中的Content-Type字段来获知请求中的消息主体是用何种方式进行编码,再对消息主体进行解析。具体的编码方式包括:

    application/x-www-form-urlencoded 最常见post提交数据的方式,以form表单形式提交数据。
    application/json 以json串提交数据。
    multipart/form-data 一般使用来上传文件。

    一、 以form表单发送post请求

    Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。
    例:

    1
    2
    3
    4
    5
    6
    7
    8
    # -*- coding: utf-8 -*-
    # author:Gary
    import requests
     
    url = 'http://httpbin.org/post' # 一个测试网站的url
    data = {'key1': 'value1', 'key2': 'value2'} # 你发送给这个的数据
    r = requests.post(url, data=data) # 使用requests的post方法,data接受你想发送的数据
    print(r.text) # 查看返回的内容

    输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    “args”: {},
    “data”: “”,
    “files”: {},
     #你提交的表单数据
    “form”: {
    “key1”: “value1”,
    “key2”: “value2”
    },
    “headers”: {
    ……
    “Content-Type”: “application/x-www-form-urlencoded”,
    ……
    },
    “json”: null,
    ……
    }

     可以看到,请求头中的Content-Type字段已设置为application/x-www-form-urlencoded,且data = {‘key1': ‘value1', ‘key2': ‘value2'}以form表单的形式提交到服务端,服务端返回的form字段即是提交的数据。

    二、 以json形式发送post请求

    可以将一json串传给requests.post()的data参数,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # -*- coding: utf-8 -*-
    # author:Gary
    import requests
    import json
     
    url = 'http://httpbin.org/post' # 一个测试网站的url
    json_data = json.dumps({'key1': 'value1', 'key2': 'value2'}) # 你发送给这个的数据,数据格式转为json
    r = requests.post(url, data=json_data) # 使用requests的post方法,data接受你想发送的数据
    print(r.text) # 查看返回的内容

    输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    {
    “args”: {},
    “data”: “{”key2”: ”value2”, ”key1”: ”value1”}”,
    “files”: {},
    “form”: {},
    “headers”: {
    ……
    “Content-Type”: “application/json”,
    ……
    },
    “json”: {
    “key1”: “value1”,
    “key2”: “value2”
    },
    ……
    }

    可以看到,请求头的Content-Type设置为application/json,并将json_data这个json串提交到服务端中。

    三、 以multipart形式发送post请求(上传文件)

    Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可。

    1
    2
    3
    4
    5
    6
    7
    8
    # -*- coding: utf-8 -*-
    # author:Gary
    import requests
     
    files = {'file': open('report.txt', 'rb')} # 目录下得有report.txt文件才能上传,rb是指以二进制格式打开一个文件用于只读。
    r = requests.post(url, files=files) # 通过files参数指定你想发送的文件的内容
    print(r.text)

    输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    {
    “args”: {},
    “data”: “”,
    “files”: {
    file”: “Hello world!”
    },
    “form”: {},
    “headers”: {……
    “Content-Type”: “multipart/form-data; boundary=467e443f4c3d403c8559e2ebd009bf4a”,
    ……
    },
    “json”: null,
    ……
    }
     

    文本文件report.txt的内容只有一行:Hello world!,从请求的响应结果可以看到数据已上传到服务端中。

  • 相关阅读:
    通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三)
    通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(二)
    stepper组件使用
    解决循环删除list中元素ConcurrentModificationException异常问题
    简单选择排序SelectSort
    用js写出简单的计算器
    在一个项目中,添加.js文件出现错误
    弹出当前值以及修改
    映射:表之间的关联
    置换(用递归的方法执行置换)
  • 原文地址:https://www.cnblogs.com/tonyxiao/p/14436129.html
Copyright © 2020-2023  润新知