现接触到的很少,详细的官方教程地址:

requests官方指南文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
requests高级指南文档:http://docs.python-requests.org/zh_CN/latest/user/advanced.html#advanced

1.安装request,bs4

pip install requests
pip install bs4

2.代码文档中调用

import requests
from bs4 import BeautifulSoup

3.发送http请求

r = requests.get(‘https://github.com/timeline.json’) #GET请求
r = requests.post(“http://httpbin.org/post”) #POST请求
r = requests.put(“http://httpbin.org/put”) #PUT请求
r = requests.delete(“http://httpbin.org/delete”) #DELETE请求
r = requests.head(“http://httpbin.org/get”) #HEAD请求
r = requests.options(“http://httpbin.org/get”) #OPTIONS请求

4.get方法

r = requests.get('http://dict.baidu.com/s', params={'wd':'python'})  #带参数的GET请求
#等于http://dict.baidu.com/s?wd=python
r = requests.get(URL, params={'ip': '8.8.8.8'}, timeout=1) #设置超时时间
r = requests.get('https://httpbin.org/hidden-basic-auth/user/passwd', auth=HTTPBasicAuth('user', 'passwd'))  #基本身份认证(HTTP Basic Auth)
r = requests.get(URL, auth=HTTPDigestAuth('user', 'pass')) #摘要式身份认证(HTTP Digest Auth)
cookies = {'testCookies_1': 'Hello_Python3', 'testCookies_2': 'Hello_Requests'}
r = requests.get(url, cookies=cookies)
#带cookies的请求
r = requests.get('http://www.baidu.com/link', allow_redirects = False) #禁止URL跳转
proxies = {
"http": "http://10.10.1.10:3128",
"http": "http://user:pass@10.10.1.10:3128/",
"https": "http://10.10.1.10:1080",
}
r = requests.get("http://www.baidu.com", proxies=proxies) #代理访问
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, compress',
'Accept-Language': 'en-us;q=0.5,en;q=0.3',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
r = requests.get('http://www.baidu.com', headers = headers)
#自定义head请求页面

5.POST方法

files = {'file': open('/home/1.mpg', 'rb')}
r = requests.post(url, files=files)
#POST上传文件
data = {'some': 'data'}
headers = {'content-type': 'application/json',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
r = requests.post('https://www.baidu.com', data=data, headers=headers)
#POST自定义HEAD提交数据
r = requests.post('https://www.baidu.com', data=json.dumps({'some': 'data'}))
#POST提交JSON数据

最新文章

  1. Linux笔记:使用Vim编辑器
  2. js 闭包之一
  3. PHP数据的序列化/反序列化
  4. iOS开发--完整项目
  5. CSS3选择器:nth-child和:nth-of-type之间的差异
  6. BZOJ 1004 Cards(Burnside引理+DP)
  7. gradle使用国内源
  8. Oracle Linux Server 7安装VMwareTools问题
  9. MVC进阶之路:依赖注入(Di)和Ninject
  10. C#、ASP.NET、WinForm - 实现邮件发送的功能
  11. 在web浏览器中判断app是否安装并直接打开
  12. mysql主从备份及常见问题处理
  13. C# 多线程传递多个参数
  14. 工具:从一个文件夹中复制jar到另一个文件夹中
  15. 集群增量会话管理器——DeltaManager
  16. 第二周 IP通信基础回顾
  17. 系统不支持WP开发
  18. PostgreSQL主要优势
  19. python的类
  20. c++多态性详解(转)

热门文章

  1. 问题解决:IDEA右键选择new新文件的时候没有JSP文件选项解决
  2. Boyer and Moore Fast majority vote algorithm(快速选举算法)
  3. python中列表的常用操作增删改查
  4. Scala基础(1)
  5. 通用shellcode
  6. CentOS-7.x Yum Repo Mirror
  7. 父类与子类this相关问题
  8. p4语言编程环境安装
  9. Vue.js 相关知识(脚手架)
  10. PAT甲题题解-1078. Hashing (25)-hash散列