基本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. Arduino LiquidCrystal Library Bug Report #174181

    Arduino LiquidCrystal Character LCD Driver Library BUG Report #174181 by Conmajia Effected Devices H ...

  2. this web application instance has been stopped already.

    this web application instance has been stopped already. Could not load oracle/sql/converter_xcharset ...

  3. 财付通API

    开发财付通API的步骤: 1.首先开发财付通API时先获取商户号和密钥: 财付通测试号:商户号String partner = "1900000109";密钥String key ...

  4. JS + HTml 时钟代码实现

    JS+ Html 画布实现的时钟效果图: 闲来无聊做了一个网页的时钟,效果模拟传统时钟的运行方式, 运用了html 的画布实现指针,背景图片引用了网络图片. 具体原理: 首先将时钟分为四个不同区域,对 ...

  5. [ext4]09 磁盘布局 - superblock备份机制

    如果sparse_super特性flag被设置(即开启了sparse_super特性),那么super_block和组描述符的副本只会保存在group索引为0或3.5.7的整数幂. 如果没有设置spa ...

  6. stl_各容器的总结

    一.stl容器总结: 1.以下的操作是在一千万的数据下操作.copy 都是在足够的空间下进行的copy, 测量方式: std::clock_t start = std::clock(); //待测代码 ...

  7. javascript ES3小测试

    一.温故知新 做做题,总是能有温故知新的体验.这套题是2010年的了,比较老了, http://perfectionkills.com/  还有一套http://perfectionkills.com ...

  8. ST-3- Installing and Testing IUnit, Hamcrest and Eclemma

    一.安装JUnit和Hamcrest 1.首先从办公网上下载JUnit.jar和Hamcrest.jar,并且将其放入所进行的项目的bin目录下,我将其放入了triangle项目的bin目录下. 2. ...

  9. SVN提交后自动推送消息到钉钉群

    钉钉设置机器人配置 1.进入配置机器人入口 2.添加机器人 3.测试WebHook请求 本人使用Postman进行测试 4.配置SVN 4.1 配置 Pre-commit hook 设置提交内容必须包 ...

  10. Centos6.5静默安装Oracle11g

    sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/configsetenforce 0yum -y insta ...