基本Get请求:
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. url = 'http://www.baidu.com'
  4. r = requests.get(url)
  5. print r.text

修改header的get请求:

  1. import requests
  2. headers = {"Authorization": "Bearer 4SMf3bbEWuzD8tGxM7Kg9LQr4RZY7xpEPgbHde5AKGFd63CHvNajtDN3PoACybLLqce1dwa9kld2ketBUpqwvZZG41SqPXw7Mtnr",
  3. "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
  4. "Host": "api.connector.mbed.com",
  5. "Accept": "*/*"
  6. }
  7. s = requests.get("https://api.connector.mbed.com/endpoints/",headers = headers)
  8. print s.request.headers
  9. print s.headers
  10. print s.text
带参数Get请求:
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. url = 'http://www.baidu.com'
  4. payload = {'key1': 'value1', 'key2': 'value2'}
  5. r = requests.get(url, params=payload)
  6. print r.text
POST请求模拟登陆及一些返回对象的方法:
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. url1 = 'http://www.exanple.com/login'#登陆地址
  4. url2 = "http://www.example.com/main"#需要登陆才能访问的地址
  5. data={"user":"user","password":"pass"}
  6. headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
  7. "Accept-Encoding":"gzip",
  8. "Accept-Language":"zh-CN,zh;q=0.8",
  9. "Referer":"http://www.example.com/",
  10. "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
  11. }
  12. res1 = requests.post(url1, data=data, headers=headers)
  13. res2 = requests.get(url2, cookies=res1.cookies, headers=headers)
  14. print res2.content#获得二进制响应内容
  15. print res2.raw#获得原始响应内容,需要stream=True
  16. print res2.raw.read(50)
  17. print type(res2.text)#返回解码成unicode的内容
  18. print res2.url
  19. print res2.history#追踪重定向
  20. print res2.cookies
  21. print res2.cookies['example_cookie_name']
  22. print res2.headers
  23. print res2.headers['Content-Type']
  24. print res2.headers.get('content-type')
  25. print res2.json#讲返回内容编码为json
  26. print res2.encoding#返回内容编码
  27. print res2.status_code#返回http状态码
  28. print res2.raise_for_status()#返回错误状态码
使用Session()对象的写法(Prepared Requests):
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. s = requests.Session()
  4. url1 = 'http://www.exanple.com/login'#登陆地址
  5. url2 = "http://www.example.com/main"#需要登陆才能访问的地址
  6. data={"user":"user","password":"pass"}
  7. headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
  8. "Accept-Encoding":"gzip",
  9. "Accept-Language":"zh-CN,zh;q=0.8",
  10. "Referer":"http://www.example.com/",
  11. "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
  12. }
  13. prepped1 = requests.Request('POST', url1,
  14. data=data,
  15. headers=headers
  16. ).prepare()
  17. s.send(prepped1)
  18. '''
  19. 也可以这样写
  20. res = requests.Request('POST', url1,
  21. data=data,
  22. headers=headers
  23. )
  24. prepared = s.prepare_request(res)
  25. # do something with prepped.body
  26. # do something with prepped.headers
  27. s.send(prepared)
  28. '''
  29. prepare2 = requests.Request('POST', url2,
  30. headers=headers
  31. ).prepare()
  32. res2 = s.send(prepare2)
  33. print res2.content
另一种写法 :
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. s = requests.Session()
  4. url1 = 'http://www.exanple.com/login'#登陆地址
  5. url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址
  6. data={"user":"user","password":"pass"}
  7. headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
  8. "Accept-Encoding":"gzip",
  9. "Accept-Language":"zh-CN,zh;q=0.8",
  10. "Referer":"http://www.example.com/",
  11. "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
  12. }
  13. res1 = s.post(url1, data=data)
  14. res2 = s.post(url2)
  15. print(resp2.content)
其他的一些请求方式:
  1. >>> r = requests.put("http://httpbin.org/put")
  2. >>> r = requests.delete("http://httpbin.org/delete")
  3. >>> r = requests.head("http://httpbin.org/get")
  4. >>> r = requests.options("http://httpbin.org/get")


遇到的问题:
在cmd下执行,遇到个小错误:
  1. UnicodeEncodeError:'gbk' codec can't encode character u'\xbb' in
  2. position 23460: illegal multibyte sequence
分析:
1、Unicode是编码还是解码
  1. UnicodeEncodeError
很明显是在编码的时候出现了错误
 
2、用了什么编码
  1. 'gbk' codec can't encode character
使用GBK编码出错
 
解决办法:
确定当前字符串,比如
  1. #-*- coding:utf-8 -*-
  2. import requests
  3. url = 'http://www.baidu.com'
  4. r = requests.get(url)
  5. print r.encoding
  6. >utf-8
已经确定html的字符串是utf-8的,则可以直接去通过utf-8去编码。
  1. print r.text.encode('utf-8')
参考链接:官方文档

问题二:
TypeError: 'unicode' object is not callable报错
  1. Traceback (most recent call last):
  2. File "F:\git\mbed_webapp\webapp.py", line 12, in <module>
  3. print s.text()
  4. TypeError: 'unicode' object is not callable
解决方法:
在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了。
参考:http://www.cnblogs.com/xiongjiaji/p/3615943.html

python requests get/post的更多相关文章

  1. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  2. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  3. Python requests 安装与开发

    Requests 是用Python语言编写HTTP客户端库,跟urllib.urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求, ...

  4. Python+Requests接口测试教程(1):Fiddler抓包工具

    本书涵盖内容:fiddler.http协议.json.requests+unittest+报告.bs4.数据相关(mysql/oracle/logging)等内容.刚买须知:本书是针对零基础入门接口测 ...

  5. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

  6. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  7. 转载:python + requests实现的接口自动化框架详细教程

    转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...

  8. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  9. python+requests接口自动化测试框架实例详解教程

    1.首先,我们先来理一下思路. 正常的接口测试流程是什么? 脑海里的反应是不是这样的: 确定测试接口的工具 —> 配置需要的接口参数 —> 进行测试 —> 检查测试结果(有的需要数据 ...

  10. 使用python requests模块搭建http load压测环境

    网上开源的压力测试工具超级的多,但是总有一些功能不是很符合自己预期的,于是自己动手搭建了一个简单的http load的压测环境 1.首先从最简单的http环境着手,当你在浏览器上输入了http://w ...

随机推荐

  1. hibernate持久化框架

    Hibernate是一个优秀的持久化框架 瞬时状态:保存在内存的程序数据,程序退出后,数据就消失了,称为瞬时状态 持久状态:保存在磁盘上的程序数据,程序退出后依然存在,称为程序数据的持久状态 持久化: ...

  2. MySQL Online DDL的改进与应用

        本文简析Online DDL的实现原理与使用过程注意事项.       任何DDL操作,执行者都需要预先测试或者清晰了解这个操作会给数据库带来的影响是否是在业务期间数据库的可承受范围内,尤其是 ...

  3. MySQL操作符

    简要介绍MySQL操作符 常用: 算术运算符.比较操作符.逻辑操作符.位运算符-- 一.算术运算符 +:加 -:减 *:乘 /:除,返回商 %,mod():除,返回余数 mysql> %,mod ...

  4. 使用gulp编译sass

    之前写了一篇在ruby环境下如何编译sass的文章:<css预处理器sass使用教程(多图预警)>,随着现在前端构建工具的兴起,也学着使用这些工具来编译sass.webpack存在一个CS ...

  5. 探讨.NET Core数据加密和解密问题

    前言 一直困扰着我关于数据加密这一块,24号晚上用了接近3个小时去完成一项任务,本以为立马能解决,但是为了保证数据的安全性,我们开始去对数据进行加密,然后接下来3个小时专门去研究加密这一块,然而用着用 ...

  6. LeetCode 207. Course Schedule(拓扑排序)

    题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have p ...

  7. websoket使用Protocol Buffers3.0传输

    Protocol Buffers是Google推出的一个数据交换格式,相对于xml它的体积更小,更快,因为它是二进制传输的.3.0相对于2.0变动比较大.这些变动可以去看官方说明. 在前端使用Prot ...

  8. 利用Sinopia搭建私有npm包

    1.安装sinopia包 npm install -g sinopia 如果是Windows系统用上面的方式安装sinopia很有可能报错,推荐使用下面方式安装: npm install sinopi ...

  9. 阿里react整合库dva demo分析

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "Helvetica Neue"; color: #404040 } p. ...

  10. hdu3652 B-number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意就是求区间内能被13整除并且包含”13“的数字的个数 感觉是比较中等的数位DP题目 我用的记 ...