本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!!

  • httpbin

    httpbin这个网站能测试 HTTP 请求和响应的各种信息,
    比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,
    对 web 开发和测试很有帮助。它用 Python + Flask 编写,是一个开源项目。
    官方网站:http://httpbin.org/
    开源地址:https://github.com/Runscope/httpbin

  • get请求
    一、简单get请求数据
     import requests
    get_url = 'http://www.httpbin.org/get'
    r = requests.get(get_url) #发送get请求
    print r
    print help(r)
    print r.text #接收的内容in bytes
    print r.content #接收的内容in unicode

    二、使用python抓去百度主页

     import requests
    r = requests.get('http://www.baidu.com')
    print r.content
    #打开一个文件,并存入网页信息
    with open('baidu.html','w') as fil:
    fil.write(r.content)

    三、get传参

     #可以先通过help函数来查看下get方法的文档说明
    import requests
    # print help(requests.get)
    get_url = 'http://www.httpbin.org/get'
    myparams={'qq':''}
    # r = requests.get(url=get_url,params=myparams)
    r = requests.get(get_url,myparams)
    print r.url #请求的url,拼接url和参数
    print r.content #返回的数据
  • post请求
    一、发送请求体
    例子:
     import requests
    #先来查看下post方法的文档使用说明
    # print help(requests.post)
    #通过data来传参,相当于传的表单,在返回的数据中可以看出
    post_url = 'http://www.httpbin.org/post'
    myData={'qq':'','cnblog':'hyyq'}
    r = requests.post(url = post_url,data = myData)
    print r.url #请求的url,请求体就是参数
    print r.content #返回的数据

    输出:

     http://www.httpbin.org/post
    {
    "args": {},
    "data": "",
    "files": {},
    "form": {
    "cnblog": "hyyq",
    "qq": "34782655"
    },
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": null,
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.3s]

    二、发送json数据
    例子:

     import requests,json
    post_url = 'http://www.httpbin.org/post'
    myData={'qq':'','cnblog':'hyyq'}
    r = requests.post(url = post_url,json = myData)
    # r = requests.post(url = post_url,data = json.dumps(myData))
    print r.url #请求的url,发送json数据
    print r.content #返回的数据

    输出:

     http://www.httpbin.org/post
    {
    "args": {},
    "data": "{\"qq\": \"34782655\", \"cnblog\": \"hyyq\"}",
    "files": {},
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "36",
    "Content-Type": "application/json",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": {
    "cnblog": "hyyq",
    "qq": "34782655"
    },
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.0s]
  • 上传文件
    例子:
     #上传文件
    import requests
    # print help(requests)
    post_url = 'http://www.httpbin.org/post'
    files = {'file':open('yyc.txt','rb')} #这里rb是以读的方式用二进制格式打开
    r = requests.post(post_url,files=files)
    print r.content

    输出:

     {
    "args": {},
    "data": "",
    "files": {
    "file": "data:application/octet-stream;base64,xOO6ww=="
    },
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "147",
    "Content-Type": "multipart/form-data; boundary=8bffc774646942deaa8a314f8675d879",
    "Host": "www.httpbin.org",
    "User-Agent": "python-requests/2.18.4"
    },
    "json": null,
    "origin": "183.230.42.10",
    "url": "http://www.httpbin.org/post"
    } [Finished in 1.0s]

Python基础笔记系列十二:requests模块的简单应用的更多相关文章

  1. Python基础笔记系列十:模块

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...

  2. Python基础笔记系列十四:python无缝调用c程序

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! python语言可以对c程序代码进行调用,以弥补python语言低性能的缺 ...

  3. Python基础笔记系列十一:标准输入输出、文件读写和指针等操作

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 标准输入输出一.输入 在sublime中这个时候需要安装SublimeRE ...

  4. Python基础笔记系列一:基本工具与表达式

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 工具基础(Windows系统下)传送门:Python基础笔记系列四:工具的 ...

  5. (Python基础教程之十二)Python读写CSV文件

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  6. python学习笔记(十二)python操作redis

    1.python要操作redis 首先需要安装redis模块,然后导入才能使用 安装:pip install redis 导入:import redis 2.连接redis r = redis.Red ...

  7. python学习笔记:网络请求——requests模块

    上面讲过的urllib模块太麻烦了,还有一个比较方便的模块,就是requests模块,好用到你怀疑人生·^_^,一定要会哦 需要安装,pip install requests即可,下面是request ...

  8. Python基础笔记系列二:分支和循环

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 分支:即是if-else和if-elif-else语句 循环:即是whil ...

  9. python学习笔记-(十二)scoket编程基础

    socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Un ...

随机推荐

  1. Linux下桥接模式详解一

    注册博客园已经好长时间,一直以来也没有在上面写过文章,都是随意的记录在了未知笔记上,今天开始本着分享和学习的精神想把之前总结的笔记逐步分享到博客园,和大家一起学习,一起进步吧! 2016-09-20  ...

  2. Saltstack之SSH简介

    安装 yum install -y salt-ssh 官方文档  https://docs.saltstack.com/en/latest/topics/ssh/index.html 配置 vi /e ...

  3. setup.py

    from distutils.core import setup # 使用说明 # 执行以下命令 # python3 setup.py build # python3 setup.py sdist(这 ...

  4. mysql联合其他表做更新

    在sql server中,我们可是使用以下update语句对表进行更新: update a set a.xx= (select yy from b) where a.id = b.id ; 但是在my ...

  5. PAT 1140 Look-and-say Sequence [比较]

    1140 Look-and-say Sequence (20 分) Look-and-say sequence is a sequence of integers as the following: ...

  6. flam3 ubuntu 依赖文件

    http://packages.ubuntu.com/zh-cn/source/precise/flam3 » Ubuntu » 软件包 » precise (12.04LTS) » 源代码 » x1 ...

  7. MySQL数据库--练习

    学生选课系统 设计表关系 创建表和插入数据 /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Ve ...

  8. td中不包含汉字的字符串不换行,包含汉字的能换行的问题原因及解决方法

    今天项目中遇到一个问题,一长串的字符串如:003403FF0014E54016030CC655BC3242,但是如:中国河北省石家庄市裕华区槐安路雅清街交口 这样的就可以换行. 原因是:英文字母之间如 ...

  9. 42. Trapping Rain Water(直方图 存水量 hard)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  10. tomcat服务无响应堆栈分析

    tomcat服务突然无响应了,导出内存堆栈和线程堆栈,分析后发现是同步锁使用不合理导致的. [root@prd-dtb-web-01 ~]# [root@prd-dtb-web-01 ~]# jmap ...