python--requests--网络编程
来源:互联网 发布:映射网络驱动器 ftp 编辑:程序博客网 时间:2024/05/19 02:43
0x00 扯
一些关于requests的使用,基本上把官方说明文档的简单用法搬了过来,
官方说明文档的地址,点击跳转
0x01 基本get,post
最常用的应该就是get和post方法了吧
import requests# getres = requests.get("http://www.baidu.com")print(res.content)#postres = requests.post("http://www.baidu.com")print(res.content)
0x02 发数据的 get,post
get方法发数据有一种比较简单的方法,因为它的参数会添加到url里,所以我们可以直接构造这样的url,比如搜索 hello requests.get("https://www.baidu.com/s?ie=utf-8&wd=hello")
下面来看看正式的方法
import requests# getpayload = {"ie":"utf-8","wd":"hello"}res = requests.get("http://www.baidu.com/s",params = payload)print(res.content)# post 大概样子是这样,例子结果可能不对payload = {"ie":"utf-8","wd":"hello"}res = requests.post("http://www.baidu.com/s", data = payload)print(res.content)
0x03 头部处理
有时候需要获取头部信息或者添加头部信息,来个例子
import requests# 获取头部信息res = requests.get("http://www.baidu.com")print(res.headers)print(res.headers["ETag"])# 修改头部信息并发送headers = {"Content-Length":"3000"}res = requests.get("http://www.baidu.com",headers = headers)print(res.content)
0x04 获取响应码状态
响应状态在判断进行什么操作的时候很重要
import requestsres = requests.get("http://www.baidu.com")print(res.status_code)
0x05 查看和发送cookie
cookie可以拿来干很多的事情呀
import requests# 查看cookieres = requests.get("http://www.baidu.com")print(res.cookies)print(res.cookies["BDORZ"])# 发送cookiecookies = {"name":"hello","pass":"password"}res = requests.get("http://www.baidu.com",cookies=cookies)print(res.cookies)
0x06 会话对象session
有的时候我们需要在一个页面获取信息,之后再发给这个页面一些响应的信息
如果按照上面的方法,只是相当于重新发起了一次请求,不能达到我们的目的
这个时候我们需要用到session这个东西,就是建立一个会话来保持一些参数
import requestsurl = "http://www.baidu.com"# 建立会话s = requests.session()# 发起请求res = s.get(url)# 获取信息data = {"status":res.status_code}# 再次请求res2 = s.post(url,data)print(res2.content)
0x07 timeout
requests默认是阻塞型请求,所以设置timeout可以跳过一些无响应或者响应慢的情况 ,下面设置超时5秒
import requests# timeoutres = requests.get("http://www.baidu.com", timeout=5)
0x08 尾
官方文档和搜索引擎还是比较好的学习方式
阅读全文
0 0
- python--requests--网络编程
- python的网络编程之requests模块
- Python网络请求模块requests
- Python网络操作之requests模块
- Python网络爬虫之Requests库
- python网络爬虫1.1requests库
- python爬虫 - python requests网络请求简洁之道
- python requests
- python requests
- Python Requests
- python Requests
- python Requests
- python requests
- Requests-Python
- python的requests在网络请求中添加cookies参数
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- Python网络爬虫requests、bs4爬取空姐网图片
- Python网络爬虫——Requests第三方库
- 第一个Java程序
- Faster-R-CNN(Python).2:COCO数据集annotation内容
- qt移植相关
- cgroup使用案例
- C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码
- python--requests--网络编程
- 电商用户行为分析大数据平台相关系列9-用户访问session的模块介绍
- 学习笔记TF013:卷积、跨度、边界填充、卷积核
- 浅谈数据挖掘与机器学习
- 1006.换个格式输出整数
- 单例模式
- chapter2-无向图
- 中值滤波(一句话概括)
- 《曼昆经济学原理》书中术语详解2