[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 ...
随机推荐
- Tarjan算法整理
众所周知,tarjan是个非常nb的人,他发明了很多nb的算法,tarjan算法就是其中一个,它常用于求解强连通分量,割点和桥等.虽然具体实现的细节不太一样,但是大体思路是差不多的.先来说一下大体思路 ...
- customizable route planning 工业界地图产品的路径规划
https://www.microsoft.com/en-us/research/publication/customizable-route-planning/?from=http%3A%2F%2F ...
- 10.1 ‘The server's host key is not cached in the registry’
10.1 ‘The server's host key is not cached in the registry’ This error message occurs when PuTTY conn ...
- Vue中解决路由切换,页面不更新的实用方法
前言:vue-router的切换不同于传统的页面的切换.路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新,也就是我们口中的页 ...
- Java ——Scanner
本节重点思维导图 import java.util.Scanner;//导包 public class Demo { public static void main(String[] args) { ...
- OO第四单元单元总结
目录 1.本单元两次作业的架构设计 2.四个单元中架构设计及OO方法理解的演进 3.测试理解与实践的演进 4.课程收获 5.给课程的改进建议 1.本单元两次作业的架构设计 第四单元的两次作业,我的表现 ...
- Linux的tail命令查看文件
小文件一般用cat 查看,但是如果文件内容过多,用cat就不合适了 可以用tail命令 # 默认显示文件最后十行 tail a.txt # 监视文件的尾部内容,默认十行, 可以-n 20显示20行 ...
- 前端 CSS的选择器 属性选择器
属性选择器,字面意思就是根据标签中的属性,选中当前的标签. 属性选择器 通常在表单控件中 使用比较多 根据属性查找 /*用于选取带有指定属性的元素.*/ <!DOCTYPE html> & ...
- linux shell 中的数组的取值 遍历 替换 删除操作
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- synchronized的底层实现原理
转自:http://www.cnblogs.com/paddix/p/5367116.html 如果对上面的执行结果还有疑问,也先不用急,我们先来了解Synchronized的原理,再回头上面的问题就 ...