requests.sessions.Session() 和 requests.Session()是相同的

结论:

(1)requests.Session   =   requests.sessions.Session  

(2)一般更建议使用 requests.Session() 来创建Session对象 (官方文档中的例子也是用requests.Session())

requests库的 __ini__.py 中,有这样一行

from .sessions import Session, session

即 requests.Session   =   requests.sessions.Session

--------------

之前的另一篇:https://blog.csdn.net/bigcarp/article/details/123276382 (requests.get 跟 request.session().get 区别)

本来想看requests.session() 和requests.Session()的区别,查看源代码见是一样的:

但源码注释中建议使用 requests.sessions.Session() 而 实际中大多数人使用requests.Session(),且官方文档中的例子也是用requests.Session()。于是奇怪为什么源码注释中建议requests.sessions.Session()。GPT解释说requests.sessions.Session()是requests库的内部实现细节,可以用于创建Session对象,但它通常不作为用户主要的使用方式,而是作为内部实现的一部分。用户应该遵循官方文档中提供的建议,使用 requests.Session() 来创建Session对象,以确保与库的稳定API一致,而不受内部实现的影响。


def session():
    """
    Returns a :class:`Session` for context-management.

    .. deprecated:: 1.0.0

        This method has been deprecated since version 1.0.0 and is only kept for
        backwards compatibility. New code should use :class:`~requests.sessions.Session`
        to create a session. This may be removed at a future date.

    :rtype: Session
    """
    return Session()

requests的具体安装过程请看:http://docs.python-requests.org/en/latest/user/install.html#install
requests的官方指南文档:http://docs.python-requests.org/en/latest/user/quickstart.html
requests的高级指南文档:http://docs.python-requests.org/en/latest/user/advanced.html#advanced


中文:

https://requests.readthedocs.io/projects/cn/zh_CN/latest/

https://requests.readthedocs.io/projects/cn/zh_CN/latest/user/quickstart.html

https://requests.readthedocs.io/projects/cn/zh_CN/latest/user/advanced.html
————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值