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

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数据

python中的requests使用小结的更多相关文章

  1. Python中使用requests和parsel爬取喜马拉雅电台音频

    场景 喜马拉雅电台: https://www.ximalaya.com/ 找到一步小说音频,这里以下面为例 https://www.ximalaya.com/youshengshu/16411402/ ...

  2. 关于python中lambda 函数使用小结

    例子: 如果定义普通函数,一般都是这样写: def:ds(x): return 2*x+1 调用即: ds(5) 如果用lambda函数就是这么写,就是一句话: g =lambda x:2*x+1 调 ...

  3. python中的变量引用小结

    python的变量都可以看成是内存中某个对象的引用.(变量指向该内存地址存储的值) 1.python中的可更改对象和不可更改对象 python中的对象可以分为可更改(mutable)对象与不可更改(i ...

  4. python中的BeautifulSoup使用小结

    1.安装 pip install beautifulsoup4 2.代码文件中导入 from bs4 import BeautifulSoup 3. 解析器 使用方法 优势 劣势 Python标准库 ...

  5. Python中的requests模块注意事项

    主要是说requests.post()方法, 参数: url :  这就不解释了 data:  如果传入的是字典类型,则字典在发出请求时会自动编码为表单形式,表单形式会将字典中的键和值进行一些操作: ...

  6. python中的编解码小结

    在用python27写文件或者上传文件时遇到这样一个问题:.在网上搜了下说加入以下三行代码可以解决: import sys reload(sys) sys.setdefaultencoding('ut ...

  7. python中字符串编码方式小结

    Python2中字符串的类型有两种:str和unicode,其中unicode是统一编码方式,它使得字符跟二进制是一一对应的,因此所有其他编码的encode都从unicode开始,而其他编码方式按照相 ...

  8. python中添加requests资源包

    1.进入资源网址下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.按下CTRL+F进行页面查找“requests” 3.点击requests-2.22. ...

  9. python中的变量对象小结2

    # .变量名和数据内容是分开存储的. # .数据保存在内存中的一个位置(地址). # .变量中保存着数据在内存中的地址. # 引用就是变量中记录数据的地址. #不可变变量,重新赋值时会重新开辟一个地址 ...

随机推荐

  1. mfc 动态为控件添加事件1

    知识点: 认识窗口过程 GetWindowLong SetWindowLong 为动态控件绑定事件 一.获取窗口过程 二.设置新窗口过程 .书写一个新窗口过程函数 窗口过程格式 LRESULT CAL ...

  2. python 回溯法 子集树模板 系列 —— 8、图的遍历

    问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...

  3. 策略模式与SPI机制,到底有什么不同?

    这里说的策略模式是一种设计模式,经常用于有多种分支情况的程序设计中.例如我们去掉水果皮,一般来说对于不同的水果,会有不同的拨皮方式.此时用程序语言来表示是这样的: if(type == apple){ ...

  4. Flutter - 给App增加启动屏幕(Splash Screen)并且设置背景颜色

    先看一下效果图,启动图最好设置为png格式的透明图,以防图片填充不满的时候背景图会非常的煞白(Flutter 默认背景色是白色). 打开android\app\src\main\res\drawabl ...

  5. javascript source map 的使用

    之前发现VS.NET会为压缩的js文添加一个与文件名同名的.map文件,一直没有搞懂他是用来做什么的,直接删除掉运行时浏览器又会报错,后来google了一直才真正搞懂了这个小小的map文件背后的巨大意 ...

  6. B1022. D进制的A+B

    除基取余法 #include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ long long a ...

  7. python类属性在继承中的修改的影响

    class A(object): x = 1 class B(A): pass class C(A): pass # 通过父类修改类属性,子类继承的类属性也改变 A.x = 3 print(A.x, ...

  8. Google I/O 2018大会小结

    Google I/O 2018大会于北京时间5月9日凌晨1点,在美国山景城Shoreline Amphitheatre(圆形剧场)举办.看了一下录播,字幕延迟,全程靠听,下面对上午的主会进行一个小结. ...

  9. shell 的 export命令

    export 功能说明:设置或显示环境变量.语 法:export [-fnp][变量名称]=[变量设置值]补充说明:在shell中执行程序时,shell会提供一组环境变量.export可新增,修改或删 ...

  10. [!] CocoaPods could not find compatible versions for pod "Folly"问题举例

    $ pod install 后出现下面错误: [!] CocoaPods could not find compatible versions for pod "Folly": I ...