当前位置: 首页 > wzjs >正文

做医学期刊杂志网站免费的个人网站怎么做

做医学期刊杂志网站,免费的个人网站怎么做,成品超市网站,淮南做网站一、发展历程 起源 Requests 库由 Kenneth Reitz 于 2011 年创建,目标是替代 Python 标准库中的 urllib2,提供更简洁、人性化的 HTTP 客户端接口。其设计哲学是 “HTTP for Humans”。 关键版本 2011: v0.2.0 发布,支持 GET/POST 方法。2012: v1.0.0 正式发布,新增 Session…
一、发展历程
  1. 起源
    Requests 库由 Kenneth Reitz 于 2011 年创建,目标是替代 Python 标准库中的 urllib2,提供更简洁、人性化的 HTTP 客户端接口。其设计哲学是 “HTTP for Humans”。

  2. 关键版本

    • 2011: v0.2.0 发布,支持 GET/POST 方法。
    • 2012: v1.0.0 正式发布,新增 Session 对象、流式下载等功能。
    • 2014: v2.0.0 支持 Python 3,底层依赖 urllib3 库。
    • 2022: 移交至 Python Software Foundation 维护。

二、基础方法与核心功能
import requests# 1. GET 请求
response = requests.get(url="http://httpbin.org/get",params={"key": "value"},  # 查询参数headers={"User-Agent": "demo"},  # 请求头timeout=5  # 超时设置
)
print(response.text)  # 响应文本# 2. POST 请求(表单数据)
response = requests.post(url="http://httpbin.org/post",data={"name": "Alice"}  # 表单数据
)# 3. POST 请求(JSON 数据)
response = requests.post(url="http://httpbin.org/post",json={"name": "Bob"}  # 自动设置 Content-Type 为 application/json
)# 4. 文件上传
files = {"file": open("test.txt", "rb")}
response = requests.post("http://httpbin.org/post", files=files)# 5. 处理响应
print(response.status_code)  # HTTP 状态码
print(response.headers)      # 响应头
print(response.json())       # 解析 JSON 数据

三、工作原理与实现机制
  1. 底层依赖
    Requests 基于 urllib3 实现,后者提供连接池、SSL/TLS 验证、重试机制等底层功能。

  2. Session 对象
    使用 Session 复用 TCP 连接,提升性能:

    with requests.Session() as session:session.get("http://httpbin.org/cookies/set/sessioncookie/123")response = session.get("http://httpbin.org/cookies")  # 自动携带 Cookie
    
  3. 请求处理流程

    • 构造 PreparedRequest 对象
    • 通过 adapters 发送请求
    • 返回 Response 对象,封装状态码、头、内容等。

四、应用领域与代码示例
1. 网页抓取(含异常处理)
import requests
from bs4 import BeautifulSouptry:response = requests.get("https://example.com",headers={"User-Agent": "Mozilla/5.0"},  # 模拟浏览器timeout=10)response.raise_for_status()  # 检查 HTTP 错误soup = BeautifulSoup(response.text, "html.parser")print(soup.title.text)
except requests.exceptions.RequestException as e:print(f"请求失败: {e}")
2. REST API 交互
import requests# 获取 GitHub 用户信息
url = "https://api.github.com/users/octocat"
response = requests.get(url)
if response.status_code == 200:data = response.json()print(f"用户名: {data['login']}, 仓库数: {data['public_repos']}")
else:print(f"错误: {response.status_code}")
3. 大文件流式下载
url = "https://example.com/large_file.zip"
response = requests.get(url, stream=True)  # 流式模式with open("large_file.zip", "wb") as f:for chunk in response.iter_content(chunk_size=8192):if chunk:  # 过滤 keep-alive 数据块f.write(chunk)
4. OAuth2 认证
# 使用 OAuth2 获取访问令牌
auth_url = "https://oauth.example.com/token"
data = {"grant_type": "client_credentials","client_id": "YOUR_CLIENT_ID","client_secret": "YOUR_SECRET"
}
response = requests.post(auth_url, data=data)
access_token = response.json()["access_token"]# 携带令牌访问 API
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get("https://api.example.com/data", headers=headers)
5. 代理设置
proxies = {"http": "http://10.10.1.10:3128","https": "http://10.10.1.10:1080"
}
requests.get("http://example.org", proxies=proxies)

五、高级特性
  1. 自定义适配器
    控制连接池大小:

    from requests.adapters import HTTPAdaptersession = requests.Session()
    adapter = HTTPAdapter(pool_connections=10, pool_maxsize=100)
    session.mount("https://", adapter)
    
  2. 请求钩子
    在请求前后插入逻辑:

    def print_url(response, *args, **kwargs):print(f"请求URL: {response.url}")requests.get("http://httpbin.org", hooks={"response": print_url})
    
  3. SSL 验证禁用
    (仅限测试环境!)

    requests.get("https://example.com", verify=False)
    

六、应用领域
  • 优势:简洁 API、丰富的功能、完善的文档。
  • 适用场景:爬虫开发、API 测试、微服务通信等。
  • 替代方案httpx(支持异步)、aiohttp(异步框架)。

七、深入实现机制

1. 连接池管理

Requests 通过 urllib3 实现高效的连接池管理,减少重复建立连接的开销。每个 Session 对象维护独立的连接池。

from requests.adapters import HTTPAdaptersession = requests.Session()
adapter = HTTPAdapter(pool_connections=5, pool_maxsize=20)  
http://www.dtcms.com/wzjs/449720.html

相关文章:

  • 网站建设四个阶段目前最好的引流推广方法
  • 上海营销网站制作重庆网站排名公司
  • 旅游网站开发系统如何优化关键词搜索
  • 贵州住房和城乡建设委员会网站抖音权重查询
  • 陕西省建设厅上海百度推广优化排名
  • 如何用php做网站管理系统it培训机构有哪些
  • wordpress 顶级分类aso关键词优化工具
  • 湖南省重点建设项目办公室网站中国十大搜索引擎网站
  • 普通企业网站营销西安seo王
  • 深圳都信建设监理有限公司网站广告平台
  • 十堰网站建设电话市场营销四大基本策略
  • 检测网站为什么打不开了哪里做网站便宜
  • 申请一个网页要多少钱长春关键词优化公司
  • 做app和网站怎样网络推广员每天的工作是什么
  • 离开东莞最新政策绍兴seo计费管理
  • 千助网站公司房产网站模板
  • 苏州做网站平台湛江百度网站快速排名
  • 个人网站代码html网站优化服务
  • 微信做引流网站seo标题优化分析范文
  • 深圳专门做写字楼的网站目前常用的搜索引擎有哪些
  • 做移动网站点击软件厦门seo俱乐部
  • 网站开发公司前置审批上海网优化seo公司
  • 新疆建设兵团门户网站智能建站模板
  • wordpress+修改邮箱搜索引擎优化的方法包括
  • 策划书案例范文页面优化的方法有哪些
  • 诚信网站认证必需做吗推广注册app拿佣金平台
  • 上海网站建设广告语沈阳专业seo关键词优化
  • 目前哪个网站建设的最好软文素材网站
  • asp.net做网站有何意义百度宁波运营中心
  • 写网站建设需求文档企业建站都有什么网站