python中requests库基础应用解析_光环大数据Python培训

上传人:gua****an 文档编号:49769906 上传时间:2018-08-02 格式:DOCX 页数:7 大小:40.69KB
返回 下载 相关 举报
python中requests库基础应用解析_光环大数据Python培训_第1页
第1页 / 共7页
python中requests库基础应用解析_光环大数据Python培训_第2页
第2页 / 共7页
python中requests库基础应用解析_光环大数据Python培训_第3页
第3页 / 共7页
python中requests库基础应用解析_光环大数据Python培训_第4页
第4页 / 共7页
python中requests库基础应用解析_光环大数据Python培训_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《python中requests库基础应用解析_光环大数据Python培训》由会员分享,可在线阅读,更多相关《python中requests库基础应用解析_光环大数据Python培训(7页珍藏版)》请在金锄头文库上搜索。

1、 光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/pythonpython 中中 requestsrequests 库基础应用解析库基础应用解析_ _光环大数据光环大数据 PythonPython 培训培训python 中 requests 库基础应用解析。python 中 requests 库基础应用解析,python 中 requests 库的基础应用,网页数据挖掘的常用库之一。也就是说最主要的功能是从网页抓取数据。python2.7 下#coding:utf-8#python 网络爬虫库 requests 库应用全解importr

2、equestsimportjsonprintu=入门=r=requests.get(https:/)#返回 Response 对象printtype(r)printr.status_code#获取返回状态printr.encoding光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/printr.cookiesprintr.raw#返回原始响应体,也就是 urllib 的 response 对象,使用r.raw.read()读取printr.content#字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩printr.

3、text#字符串方式的响应体,会自动根据响应头部的字符编码进行解码r.headers#以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回 None#*特殊方法*#r.json()#Response 中内置的 JSON 解码器#r.raise_for_status()#功能:如果失败请求(非 200 响应)抛出异常printu=基本请求=r=requests.post(“https:/httpbin.org/post“)r=requests.put(“https:/httpbin.org/put“)r=requests.delete(“https:/httpb

4、in.org/delete“)光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/r=requests.head(“https:/httpbin.org/get“)r=requests.options(“https:/httpbin.org/get“)printu=基本 GET 请求=payload=key1:value1,key2:value2#字典数据headers=content-type:application/json#header 数据r=requests.get(“https:/httpbin.org/get“,params=

5、payload,headers=headers)#payload 可以省略,会在网址中添加printr.urlprintu=基本 POST 请求=url=https:/httpbin.org/postpayload=some:datar=requests.post(url,data=json.dumps(payload)#上传 json 数据printr.texturl=https:/httpbin.org/postmyfiles=file:open(test.txt,rb)#获取文件对象光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/

6、r=requests.post(url,files=myfiles)#上传文件printr.text#withopen(test.txt)asf:#流式上传#requests.post(https:/some.url/streamed,data=f)printu=Cookies=url=https:/httpbin.org/cookiesr=requests.get(url)#响应中会包含 cookieprintr.cookiesurl=https:/httpbin.org/cookiescookies=dict(cookies_are=working)#自定义 cookie 变量r=requ

7、ests.get(url,cookies=cookies)#请求时附带 cookieprintr.textprintu=超时配置=requests.get(https:/,timeout=1)#1 秒没有响应就报错光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/printu=会话对象(持久连接)=s=requests.Session()s.get(https:/httpbin.org/cookies/set/sessioncookie/123456789)#get 方式设置 cookier=s.get(“https:/httpbin.o

8、rg/cookies“)#获取 cookieprint(r.text)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;Linuxx86_64;rv:22.0)Gecko/20100

9、101Firefox/22.0s=requests.Session()光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/s.headers.update(headers)#更新 headerr=s.get(https:/httpbin.org/headers,headers=x-test:true)#get 函数中添加的 headers 会覆盖原有同名,添加不同名的,去除值为 None 的printr.textprintu=SSL 证书验证=r=requests.get(https:/,verify=True)printr.textr=r

10、equests.get(https:/ 设置为 False 即可跳过证书验证printr.textprintu=证书=proxies=“https“:“https:/41.118.132.69:4433“光环大数据光环大数据-大数据培训知名品牌大数据培训知名品牌http:/ 光环大数据光环大数据 http:/r=requests.post(“https:/httpbin.org/post“,proxies=proxies)printr.text为什么大家选择为什么大家选择光环大数据光环大数据!大数据培训、人工智能培训、Python 培训、大数据培训机构、大数据培训班、数据分析培训、大数据可视化培训,就选光环大数据!光环大数据,聘请专业的大数据领域知名讲师,确保教学的整体质量与教学水准。讲师团及时掌握时代潮流技术,将前沿技能融入教学中,确保学生所学知识顺应时代所需。通过深入浅出、通俗易懂的教学方式,指导学生更快的掌握技能知识,成就上万个高薪就业学子。【报名方式、详情咨询报名方式、详情咨询】光环大数据官方网站报名:光环大数据官方网站报名:http:/ /mobile/mobile/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 数据挖掘与识别

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号