python requests get/post

基本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




posted on 2016-07-10 10:11  jsplyy  阅读(2998)  评论(0编辑  收藏  举报

导航