1、get请求

# -*- coding: utf-8 -*-
import requests URL_IP = "http://b.com/index.php"
pyload = {'cate':1,'id':2}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
def use_simple_requests():
r = requests.get(URL_IP, params=pyload, headers=headers)
print r.url #resuest url
print r.headers #response header
print r.text #response 内容(编码后)
print r.content #response 内容(无编码)
print r.status_code#http状态码
print r.json() #输出json数据,服务器返回的不是json内容则会报错
#print r.raw.read(100) #获取原始套接字相应,请求中需设置 stream=True use_simple_requests()

2、post请求

# -*- coding: utf-8 -*-
import requests URL_IP = "http://b.com/index.php"
pyload = {'cate':1,'id':2}
def use_simple_requests():
r = requests.post('http://httpbin.org/post', data=pyload)
print r.text use_simple_requests()

3、cookie

  服务器端:http://a.com/index.php

<?php
$cookie_name = $_COOKIE['name']; //接受客户端发来的cookie
setcookie('name',$cookie_name.'777'); //将接受的cookie值追加'777'后发给客户端
?>

  客户端:request_demo.py

# -*- coding: utf-8 -*-
import requests URL_IP = "http://b.com/index.php"
cookies = {'name':'testtest'} #设置cookie
def use_simple_requests():
r = requests.get(URL_IP, cookies=cookies) #request请求中带上cookie
print r.headers
print r.cookies['name'] #打印cookie use_simple_requests()

文档:http://docs.python-requests.org/en/master/user/quickstart/#make-a-request

[python 学习] requests 库的使用的更多相关文章

  1. Python学习--- requests库中文编码问题

    为什么会有ISO-8859-1这样的字符集编码 requests会从服务器返回的响应头的 Content-Type 去获取字符集编码,如果content-type有charset字段那么request ...

  2. 【转】使用Python的Requests库进行web接口测试

    原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...

  3. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  4. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

  5. 解决python的requests库在使用过代理后出现拒绝连接的问题

    在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...

  6. python 之Requests库学习笔记

    1.    Requests库安装 Windows平台安装说明: 直接以管理员身份打开cmd运行界面,使用pip管理工具进行requests库的安装. 具体安装命令如下: >pip instal ...

  7. 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用

    学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...

  8. python爬虫——requests库使用代理

    在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...

  9. python利用requests库模拟post请求时json的使用

    我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...

随机推荐

  1. MySQL慢日志分析之pt-query-digest

    http://www.php.cn/mysql-tutorials-357655.html 监控慢日志: pt-query-digest 切割分析慢日志 anemometer 删掉垃圾查询 pt-ki ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_10_字节输入流一次读取一个字节的原理

    原理解析 创建一个字节流,指向读取文件的第一个字节.  read找jvm,jvm找os.os去读取硬盘.,读取后指正向后移动一位

  3. 后台管理模板ACE

    要做系统,界面展示效果较简单,1个开发人员,无美工,无前端! 以前一直用bootstrap来做界面.这次也走的老路!同样适用的bootstrap界面.做完之后,功能上,我还是比较满意的,兼容IE8+, ...

  4. HslControls

    HslControls控件库的使用demo,HslControls是一个工业物联网的控件库,基于C#开发,配套HslCommunication组件可以实现工业上位机软件的快速开发,支持常用的工业图形化 ...

  5. 【Android Studio安装部署系列】十三、Android studio添加和删除Module 2

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 新建.导入.删除Module是常见的操作,这里简单介绍下. 新建Module File——New——New Module... 选中 ...

  6. Altium Designer chapter7总结

    PCB设计高级进阶中需要注意如下: (1)PCB层集合管理:对于后期的处理可以看到不同层的相关信息. (2)内电层的分割:对于多层板的设计,特别是电源层中有不同类型的电源时需要考虑电源的分割. (3) ...

  7. Cassandra commands

      Common commands:   describe keyspaces // 列出所有db use your_db; // 进去db describe tables; // 列出所有table ...

  8. 安全运维 - Linux系统维护

    命令相关 帮助信息命令:help.whatis.info.which.whereis.man 目录管理: cd.ls.mkdir.rm.chmod.mv 用户管理: groupadd.groupdel ...

  9. auto_ptr与shared_ptr

    注: 从c++11开始, auto_ptr已经被标记为弃用, 常见的替代品为shared_ptr shared_ptr的不同之处在于引用计数, 在复制(或赋值)时不会像auto_ptr那样直接转移所有 ...

  10. mysql 主从 设置

    总结:1.如果是虚拟克隆mysql 请注意auto.cnf的uuid保证不一样,即删除auto.cnf 重新启动即可2.默认安装的mysql配置文件mysqld.cnf可能绑定了127.0.0.1 只 ...