[python 学习] requests 库的使用
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 库的使用的更多相关文章
- Python学习--- requests库中文编码问题
为什么会有ISO-8859-1这样的字符集编码 requests会从服务器返回的响应头的 Content-Type 去获取字符集编码,如果content-type有charset字段那么request ...
- 【转】使用Python的Requests库进行web接口测试
原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
- 解决python的requests库在使用过代理后出现拒绝连接的问题
在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...
- python 之Requests库学习笔记
1. Requests库安装 Windows平台安装说明: 直接以管理员身份打开cmd运行界面,使用pip管理工具进行requests库的安装. 具体安装命令如下: >pip instal ...
- 一起学爬虫——通过爬取豆瓣电影top250学习requests库的使用
学习一门技术最快的方式是做项目,在做项目的过程中对相关的技术查漏补缺. 本文通过爬取豆瓣top250电影学习python requests的使用. 1.准备工作 在pycharm中安装request库 ...
- python爬虫——requests库使用代理
在看这篇文章之前,需要大家掌握的知识技能: python基础 html基础 http状态码 让我们看看这篇文章中有哪些知识点: get方法 post方法 header参数,模拟用户 data参数,提交 ...
- python利用requests库模拟post请求时json的使用
我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...
随机推荐
- UVALive 3958 Weird Numbers (负进制数)
Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...
- CSS札记(一):CSS选择器
一.语法规则 选择器{ 属性1:属性值1; 属性2:属性值2; ...... } /*注释*/ 二.如何在html中应用CSS 1. 外部引用css文件 css文件:css/layout.css(cs ...
- 字节对齐#pragma pack
这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n) 作用:C编译器将按照n个字节对 ...
- 北风设计模式课程---依赖倒置原则(Dependency Inversion Principle)
北风设计模式课程---依赖倒置原则(Dependency Inversion Principle) 一.总结 一句话总结: 面向对象技术的根基:依赖倒置原则(Dependency Inversion ...
- P1080国王游戏
传送 最大值最小什么的一看就是二分了qwq 然鹅并不知道怎么检查,所以我们换个思路 我们要求出最小的最大值,这肯定和大臣的排列有关,会不会有什么规律? 先看看只有两个大臣的情况 排列:1 2,ans1 ...
- vue+ts修改父组件属性的写法。
部分代码如下: 父组件: <coupon :modifyFlag.sync="flag" /> data() { return { fl ...
- (转)flexpaper 参数
本文转载自:http://blog.csdn.net/z69183787/article/details/18659913 Flexpaper可能用到如下参数 SwfFile (String) 需 ...
- 《图解 TCP-IP(第 5 版)》
第一章 网络基础知识 计算机网络根据规模可以分为:广域网(WAN: Wide Area Network)和局域网(LAN: Local Area Network) 协议的标准化: 国际标准化组织(IS ...
- JS 弹出网页 (不显示地址栏,工具栏) 网页去掉地址栏
JS 弹出网页 (不显示地址栏,工具栏) 网页去掉地址栏 window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 基本语法: ...
- NornJ-javascript模版引擎
NornJ-javascript模版引擎 NornJ是一个渲染效率高,语法可读性好,可扩展性超强,适用场景丰富的javascript模板引擎. 学习网址:https://www.npmjs.com/p ...