安装
- pip install requests
快速上手
发送请求
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. # 发送get请求 7. r = requests.get('https://api.github.com/events') 8. print(r) 9. 10. # 发送post请求 11. r = requests.post('http://httpbin.org/post', data={'key': 'value'}) 12. print(r) 13. 14. # 发送PUT,DELETE,HEAD 以及 OPTIONS请求 15. r1 = requests.put('http://httpbin.org/put', data={'key': 'value'}) 16. r2 = requests.delete('http://httpbin.org/delete') 17. r3 = requests.head('http://httpbin.org/get') 18. r4 = requests.options('http://httpbin.org/get') 19. print(r1, r2, r3, r4)
传递URL参数
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. # 例如, http://httpbin.org/get?key=val&key2=value2 7. payload = {'key1': 'value1', 'key2': 'value2'} 8. r = requests.get("http://httpbin.org/get", params=payload) 9. print(r.url) 10. 11. # 列表作为值传入 12. payload = {'key1': 'value1', 'key2': ['value2', 'value3']} 13. 14. r = requests.get('http://httpbin.org/get', params=payload) 15. print(r.url)
响应内容
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. r = requests.get('https://api.github.com/events') 7. # 文本格式 8. print(r.text) 9. print(type(r.text)) 10. # 二进制格式 11. print(r.content) 12. print(type(r.content)) 13. # json格式 14. print(r.json()) 15. print(type(r.json())) 16. # 原始响应内容 17. print(r.raw) 18. print(type(r.raw))
添加请求头
1. # 添加请求头,只要简单地传递一个 dict 给 headers 参数就可以了 2. url = 'https://api.github.com/some/endpoint' 3. headers = {'user-agent': 'my-app/0.0.1'} 4. r = requests.get(url, headers=headers)
发送post请求
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import json 5. 6. import requests 7. 8. # 发送post请求 9. # 1.表单形式的数据,只需简单地传递一个字典给 data 参数 10. payload = {'key1': 'value1', 'key2': 'value2'} 11. 12. r = requests.post("http://httpbin.org/post", data=payload) 13. print(r.text) 14. 15. # 还可以为 data 参数传入一个元组列表。在表单中多个元素使用同一 key 的时候,这种方式尤其有效 16. payload = (('key1', 'value1'), ('key1', 'value2')) 17. r = requests.post('http://httpbin.org/post', data=payload) 18. print(r.text) 19. 20. # 2.请求体-json格式数据 21. url = 'https://api.github.com/some/endpoint' 22. payload = {'some': 'data'} 23. 24. r = requests.post(url, data=json.dumps(payload)) 25. 26. # 使用 json 参数直接传递 27. url = 'https://api.github.com/some/endpoint' 28. payload = {'some': 'data'} 29. 30. r = requests.post(url, json=payload) 31. 32. # 3.上传文件 33. url = 'http://httpbin.org/post' 34. files = {'file': open('report.xls', 'rb')} 35. 36. r = requests.post(url, files=files) 37. print(r.text) 38. 39. # 显式地设置文件名,文件类型和请求头 40. url = 'http://httpbin.org/post' 41. files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})} 42. r = requests.post(url, files=files) 43. print(r.text)
响应信息
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. r = requests.get('http://httpbin.org/get') 7. # 响应状态码 8. print(r.status_code) 9. # 响应头 10. print(r.headers) 11. print(r.headers['Content-Type']) # 获取响应头某些字段值 12. print(r.headers.get('content-type'))
Cookie
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. # 添加cookies 7. url = 'http://httpbin.org/cookies' 8. cookies = dict(cookies_are='working') 9. r = requests.get(url, cookies=cookies) 10. print(r.text) 11. 12. # 获取cookies 13. # print(r.cookies.get('cookie_name')) # 示例,运行会报错 14. 15. # Cookie 的返回对象为 RequestsCookieJar,它的行为和字典类似,但接口更为完整,适合跨域名跨路径使用 16. jar = requests.cookies.RequestsCookieJar() 17. jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies') 18. jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere') 19. url = 'http://httpbin.org/cookies' 20. r = requests.get(url, cookies=jar) 21. print(r.text)
超时与重定向
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/5/5 3. # @Author : 大海 4. import requests 5. 6. r = requests.get('http://github.com') 7. # 请求url 8. print(r.url) 9. # 请求历史 10. print(r.history) 11. 12. # 重定向设置allow_redirects True 允许,False 不允许 13. r = requests.get('http://github.com', allow_redirects=False) 14. print(r.status_code) 15. print(r.history) 16.