Python之request模块-基础用法
Request模块参考中文手册:https://requests.readthedocs.io/zh_CN/latest/
Request模块
1.查看pip已装包(模块)的安装信息(模块的路径、版本、模块说明)
语法:pip show 模块名
例子:pip show requests

2.发送请求
当然还有其他的请求方式,就不一一列举了。如:request.post、request.delete等等
# 发送GET请求,不携带参数
request.get("http://www.baidu.com") //返回一个响应对象
# 发送GET请求,携带参数
request.get("https://www.baidu.com/s",params={"wd":"python"})
# 带请求头参数
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"}
request.get("http://www.baidu.com",headers=headers)
# 发送cookie
cookies = {"name":"haha"}
request.get("http://www.baidu.com",cookie=cookies)
# 禁用重定向
request.get("http://www.baidu.com",all_redirects=False)
# 设置请求响应时间
request.get("http://www.baidu.com",timeout=0.1)
# POST请求
url = 'https://api.github.com/some/endpoint'
data = {"name":"haha"}
request.post(url,data=data)
3.响应对象
r = request.get("http:///www.baidu.com")
#查看响应头的信息
r.headers
#查看响应的编码
print(r.encoding) //ISO-8859-1
#设置编码
r.encoding = "UTF-8"
#以字符串的形式返回响应的内容
print(r.text)
#以字节的形式返回响应的内容
print(r.content)
# 状态码
r.status_code
# 获取cookie信息
r.cookies
#获取cookie的某个name的值
r.cookies["BAIDUID"]
# 请求历史记录
r.history
Python之request模块-基础用法的更多相关文章
- python中logging模块的用法
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- 【Python爬虫】selenium基础用法
selenium 基础用法 阅读目录 初识selenium 基本使用 查找元素 元素互交操作 执行JavaScript 获取元素信息 等待 前进后退 Cookies 选项卡管理 异常处理 初识sele ...
- Python的logging模块基本用法
Python 的 logging 模块的简单用法 在服务器部署时,往往都是在后台运行.当程序发生特定的错误时,我希望能够在日志中查询.因此这里熟悉以下 logging 模块的用法. logging 模 ...
- Python学习之模块基础
模块就是程序 编写以下简单代码 print('hello python') 并将py文件保存在c盘的python(假设新建)文件下,通过pycharm的Terminal 或者windom命令窗口调出p ...
- python之logging模块简单用法
前言: python引入logging模块,用来记录自己想要的信息.print也可以输入日志,但是logging相对print来说更好控制输出在哪个地方.怎么输出以及控制消息级别来过滤掉那些不需要的信 ...
- python中ConfigParse模块的用法
ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 配置文件的格式是: [...]包含的叫section section 下有option=value这样的键值 ...
- (数据科学学习手札53)Python中tqdm模块的用法
一.简介 tqdm是Python中专门用于进度条美化的模块,通过在非while的循环体内嵌入tqdm,可以得到一个能更好展现程序运行过程的提示进度条,本文就将针对tqdm的基本用法进行介绍. 二.基本 ...
- selenium模块基础用法详解
目录 selenium模块 官方文档 介绍 安装 有界面浏览器 无界浏览器 selenium+谷歌浏览器headless模式 基本使用 选择器 基本用法 xpath 获取标签属性 等待元素被加载 隐式 ...
- Beautifulsoup模块基础用法详解
目录 Beautifulsoup模块 官方中文文档 介绍 基本使用 遍历文档树 搜索文档树 五种过滤器 **find_all( name , attrs , recursive , text , ** ...
随机推荐
- 阿里云serverless使用笔记
1.使用api网关服务,创建完api后,测试时,需要传“请求签名”,否则会报401 ‘Empty Signature’错误.相关文档<错误编码表,请求签名>.(错误信息放置与响应头的‘x- ...
- maxima已知方程,计算结果
- source ~/.bashrc
编辑命令: gedit ~/.bashrc source ~/.bashrc 每次修改.bashrc后,使用source ~/.bashrc(或者 . ~/.bashrc)就可以立刻加载修改后的设置, ...
- MySQL日记
MySQL日记 MySQL——day01:https://www.cnblogs.com/noonjuan/diary/2019/07/24/11241543.html MySQL——day02:ht ...
- Linux性能优化实战学习笔记:第三十四讲
一.上节回顾 上一节,我带你学习了 Linux 网络的基础原理.简单回顾一下,Linux 网络根据 TCP/IP模型,构建其网络协议栈.TCP/IP 模型由应用层.传输层.网络层.网络接口层等四层组成 ...
- [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- netcore与ef资料收集
http://www.cnblogs.com/cgzl/p/7661805.html https://www.cnblogs.com/cgzl/p/7675485.html https://www.c ...
- sync 异步编程
using System; using System.Net; using System.Threading; using System.Threading.Tasks; namespace Cons ...