现接触到的很少,详细的官方教程地址:
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和parsel爬取喜马拉雅电台音频
场景 喜马拉雅电台: https://www.ximalaya.com/ 找到一步小说音频,这里以下面为例 https://www.ximalaya.com/youshengshu/16411402/ ...
关于python中lambda 函数使用小结
例子: 如果定义普通函数,一般都是这样写: def:ds(x): return 2*x+1 调用即: ds(5) 如果用lambda函数就是这么写,就是一句话: g =lambda x:2*x+1 调 ...
python中的变量引用小结
python的变量都可以看成是内存中某个对象的引用.(变量指向该内存地址存储的值) 1.python中的可更改对象和不可更改对象 python中的对象可以分为可更改(mutable)对象与不可更改(i ...
python中的BeautifulSoup使用小结
1.安装 pip install beautifulsoup4 2.代码文件中导入 from bs4 import BeautifulSoup 3. 解析器 使用方法 优势 劣势 Python标准库 ...
Python中的requests模块注意事项
主要是说requests.post()方法, 参数: url : 这就不解释了 data: 如果传入的是字典类型,则字典在发出请求时会自动编码为表单形式,表单形式会将字典中的键和值进行一些操作: ...
python中的编解码小结
在用python27写文件或者上传文件时遇到这样一个问题:.在网上搜了下说加入以下三行代码可以解决: import sys reload(sys) sys.setdefaultencoding('ut ...
python中字符串编码方式小结
Python2中字符串的类型有两种:str和unicode,其中unicode是统一编码方式,它使得字符跟二进制是一一对应的,因此所有其他编码的encode都从unicode开始,而其他编码方式按照相 ...
python中添加requests资源包
1.进入资源网址下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.按下CTRL+F进行页面查找“requests” 3.点击requests-2.22. ...
python中的变量对象小结2
# .变量名和数据内容是分开存储的. # .数据保存在内存中的一个位置(地址). # .变量中保存着数据在内存中的地址. # 引用就是变量中记录数据的地址. #不可变变量,重新赋值时会重新开辟一个地址 ...
随机推荐
JavaScript判断数据类型
JavaScript中判断数据类型的方式有三种: 1.typeof typeof 1; //"number" typeof "abc"; //" ...
Html 开发工具 之Hbulider
下载地址 : 百度搜索该名字即可 或 点击此处 选择 文件->新建Web项目 输入项目名称即可 如下图 它会自动生成一些 文件夹 或文件,不需要的删除即可.
link和import的区别
1.link是xhtml标签,除了可以加载css外,还可以加载定义其他的事务:@import只属于css的范畴,只能加载css: 2.link引用CSS时,在页面载入时同时加载:@import需要页面 ...
关于ArcGIS10.0中的栅格计算中的函数
版本升级确实很重要,在ArcGIS10.1中计算成功的,在10.0中出了问题. 问题 在进行栅格计算时,计算公式很简单,包括两个Ln函数: "-22.73 + 11.1 * Ln(5) + ...
【codevs1993】草地排水 最大流
[codevs1993]草地排水 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段 ...
NUTZ中处理系统未捕获异常
关键内容 mvc-chain.js ViewProcessor ai.setFailView(“redirect:/sysError.html”); log.error(this.trrowableT ...
C++中typedef enum 和 enum
在C++中,这两种定义枚举类型的关键字用法和效果相同,推荐使用前者.typedef enum多用在C语言中. 在C语言中,如果使用typedef enum定义一个枚举类型,比如: typedef en ...
The open source JavaScript graphing library that powers Plotly
https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...
luogu2634 聪聪可可 (树形dp)
要求出两点间距离==0(mod3) 的数量,然后除以(n*n) 设f[i][j]为i的子树到i的距离==j(mod3)的数量,然后做树形dp即可 因为要最简,所以要求一下gcd,然后除下去 #incl ...
解决ora-01034和ora-27101错误
使用plsql登录oracle数据库,提示如下错误: 定位原因:tnsnames.ora文件中数据库的配置参数有误所致 解决办法:将SERVICE_NAME修改为SID即可