Requests请求库

import requests
# -*- coding:utf8 -*-
# -*- coding:utf8 -*-
# 工程路径:3 requests请求库使用.py
# 工程日期:8/6/2019
# 工程目标:
"""
requests 7个主要方法:
requests.request(): 构造一个请求,支撑一下各方法的基础方法
requests.get(): 获取HTML网页的主要方法,对应HTTP的GET
requests.head(): 获取HTML网页头的信息方法,对应HTTP的HEAD
requests.post(): 向HTML网页提交POST请求方法,对应HTTP的POST
requests.put(): 向HTML网页提交PUT请求的方法,对应HTTP的RUT
requests.patch(): 向HTML网页提交局部修改请求,对应于HTTP的PATCH
requests.delete(): 向HTML页面提交删除请求,对应HTTP的DELETE

13个参数 requests.request(method,url,**kwargs)
method:请求方式,对应get/put/post等7种
requests.method(url, **kwargs)
url:拟获取页面的url链接

**kwargs:控制访问参数,共13个,均为可选项:
params: 字典或字节序列,作为参数增加到url中
data: 字典,字节序列或文件对象,作为Request的内容
json: JSON格式的数据,作为Request的内容
headers: 字典,HTTP定制头(模拟浏览器进行访问)
cookies: 字典或CpplieJar,Request中的cookie
auth: 元祖,支持HTTP认证功能
files: 字典类型,传输文件
timeout: 设定超时时间,秒为单位
proxies: 字典类型,设定访问代理服务器,可以增加登陆认证
allow_redirects:True//False, 默认为True,重定向开关
stream:True/False,默认为True, 获取内容立即下载开关
verify:True/False,默认为True, 认证SSL证书开关
cert: 本地SSL证书路径
"""
#%%
import requests
response = requests.get('http://www.baidu.com/')
print(type(response)) #查看response的类型
print(requests.status_codes) # 状态码
print(response.text) # 输出响应内容
print(response.headers) # 返回响应头
print(requests.cookies)

#%% get 方法传传参 添加参数,headers等
import requests
data = {
"s?tn" : "02003390_30_hao_pg",
'wd':'美女'
}
header = {}
response = requests.get('http://www.taobao.com/',params=data )
print(response.text)
print(response.url)
#%% 解析json
import requests
import json
response = requests.get('http://www.baidu.com/')
print(response.json())

#%% 获取二进制流数据
import requests
response = requests.get('https://hbimg.huabanimg.com/6519f3b9d79be866403eb8d33ea5fa9ca5e3e5a2e40f6-Fzf6yq_fw658')
with open('tupian.jpg','wb') as f:
f.write(response.content)
f.close()

#%% 响应属性
import requests
response = requests.get('http://www.baidu.com')
print(response.content)
print(response.url)
print(response.headers)
print(response.text)
print(response.cookies)
print(response.encoding)
print(response.history)
print(response.next)

#%%状态码判断
import requests
response = requests.get('http://www.baidu.com')
if response.status_code == requests.codes.ok:
print("ok")

#%% 取cookies
import requests
response = requests.get('http://www.baidu.com')
print(response.cookies)
for key, value in response.cookies.items():
print(key + '=' + value)

#%% 会话维持 session

#%% 证书验证
# 大部分的网站为https网站, 需要证书验证 非官方认证的证书网站会发生ssl报错
# 为避免该类型的异常抛出,将证书的参数设置为false
import requests
# response = requests.get('https://www.12306.cn',verify = False)
response = requests.get('https://www.12306.cn')
print(response.status_code)
print(response.content)

#%% 代理设置
# 声明字典类型的代理集,作为代理参数传即可
import requests
proxies = {
'http':'http://127.0.0.1:1080'
#'https': 'https://127.0.1.7:1060'
}
response = requests.get('https://www.12306.cn',verify = False, proxies=proxies)
print(response.content)

#%% 异常处理
# requestsexception(ioerror)
# 父类异常为requestexception 继承IOerror
# requests的异常也可以捕获子类 connectionerror、urlrequerd、toomanyredirects、httperror
# connecttimeout、readtimeout、timeout、sslerror、proxyerror 异常
import requests
from requests.exceptions import ReadTimeout, HTTPError, RequestException
response = requests.get('http://www.baidu.com')
try:
response = requests.get('https://www.baidu.com', timeout=0.1)
print(response.status_code)
except ReadTimeout:
print("超时错误")
except ConnectionError:
print("连接错误")
except RequestException

转载于:https://www.cnblogs.com/binyang/p/10995130.html

Requests请求库相关推荐

  1. Day02:requests请求库,selenium请求库

    一 requests请求库爬取豆瓣电影信息  - 请求url https://movie.douban.com/top250 - 请求方式 GET​- 请求头 user-agent cookies i ...

  2. day02 requests请求库爬取豆瓣电影信息+selenium请求库

    一. requests请求库爬取豆瓣电影信息 - 请求url http://movie.douban.com/top250 - 请求方式 GET - 请求头 user-agent    cookies ...

  3. Python网络请求库Requests,妈妈再也不会担心我的网络请求了(二)

    本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 极客导航 即可关注,每个工作日都有文章更新. 一.概况 接着上篇说,如果你真以为Requests网络请求库只有Get请求和Post请求 ...

  4. 爬虫之基本原理及简单使用、请求库之requests库及小案例

    文章目录 1.基本原理及简单使用 1.1.定义 1.2.爬虫的基本流程 1.3.请求与响应 1.4.Request 1.5.Response 1.6.总结 2.请求库之requests库 2.1.基本 ...

  5. python网络爬虫教程(四):强大便捷的请求库requests详解与编程实战

    上一章中,我们了解了urllib的基本用法,详情可浏览如下链接python网络爬虫教程(三):详解urllib库,但其中确实有不方便的地方,为此,我们可以使用更方便更简洁的HTTP请求库request ...

  6. Python网络请求库Requests,妈妈再也不会担心我的网络请求了(一)

    本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 极客导航 即可关注,每个工作日都有文章更新. 一.概况 网络请求可能是每门语言比较重要的一部分了,在Python语言中,虽然有urll ...

  7. requests的介绍(python的第三方HTTP请求库)

    本文目录: O.requests库介绍 一.requests的安装 二.requests库的七个主要方法 三.response对象的属性 四.理解response的编码 五.理解requests库的异 ...

  8. Python 深入浅出 - 网络请求库 Requests

    Requests 是用 Python 语言编写的,基于 urllib,采用 Apache2 Licensed 开元协议的 HTTP 库,它比 urllib 更加方便,编写爬虫和测试服务器响应数据时经常 ...

  9. HTTPX: 青出于蓝,比肩requests的新生代网络请求库

    作为新生代的网络请求库,HTTPX 不仅支持 requests 的所有操作,同时支持 异步API 及 HTTP/2.根据官网的描述,总结有如下特点: 标准的同步接口及异步支持 HTTP/1.1 和 H ...

最新文章

  1. Windows SDK 7.1 (包含directshow)安装配置
  2. Size Matters! Long-Read DNA Sequencing
  3. SAP MM公司间STO里的交货单自动创建?
  4. 在CentOS下安装apche+tomcat+mysql+php
  5. 一个NPOI导出到excel文件的范例记录
  6. laravel 报错htmlspecialchars() expects parameter 1 to be string, object given
  7. IDEA中导入一个新项目,出现了Cannot resolve symbol 'String'
  8. 瑞士Migros Ostschweiz使用RFID和EPCIS优化供应链的可视化
  9. 服务器修改开机启动项,启动项设置_服务器开机启动项
  10. 雷达信号处理——雷达系统
  11. 1、vinc = vict 胜、征服
  12. 美国佛罗里达州立大学计算机系王广老师招收人工智能全奖博士生
  13. 时光金科php_发布中国首个社区敬老宣言,共敬美好岁月-金科全国首届重阳敬老节温暖落幕...
  14. ORA-01652: 无法通过 128 (在表空间 LTE_PM_TEMP 中) 扩展 temp 段
  15. 小程序 配置域名 业务域名_使域名成为您的业务
  16. 小新14pro锐龙版虚拟机启动蓝屏重启
  17. python中set option_pd.set_option
  18. 音乐制作软件中文精简版-Nuendo 4 v4.3 WiN
  19. 【质数乘积且包含回文快速近积数】2021-11-22
  20. Google Map手机地图

热门文章

  1. boost::log::sinks::simple_event_log_backend用法的测试程序
  2. boost::fusion::traits用法的测试程序
  3. GDCM:gdcm::EncapsulatedDocument的测试程序
  4. Boost:宏BOOST_ASSERT的使用实例
  5. ITK:将样条曲线拟合到点集
  6. VTK:可视化之VectorOfActors
  7. OpenCV扫描图像对象的实例(附完整代码)
  8. OpenGL 位图字体渲染的实例
  9. C语言spirograph算法图形绘制(附完整源码)
  10. C++语言之父 Bjarne Stroustrup 简介