zoukankan      html  css  js  c++  java
  • python爬虫request库中的session

    python爬虫request库中的session

    一、总结

    一句话总结:

    The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3’s connection pooling.
    So if you’re making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection).

    二、python爬虫request库中的session

    转自或参考:python爬虫(十一) session - 方木Fengl - 博客园
    https://www.cnblogs.com/zhaoxinhui/p/12384342.html

    这是一个会话对象,对目标服务器得请求通过session来完成

     例如人人网爬取大鹏主页信息,

    # requests使用session,不用登录查看人人网大鹏信息
    
    import requests
    
    
    url='http://www.renren.com/PLogin.do'
    
    id = input('请输入用户名:')
    pw = input('请输入密码:')
    
    data = {
            
            "email": id,
            "password": pw}
    headers={
        'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
    }
    session=requests.session()
    session.post(url,data=data,headers=headers)
    response=session.get("http://www.renren.com/880151247/profile")
    with open('renren.html','w',encoding='utf-8') as fp:
        fp.write(response.text)

    在控制台输入用户名和密码之后出来结果:

     
  • 相关阅读:
    python每日作业4/21
    socket实现并发之socketserver模块的使用
    python socket粘包问题的解决
    每日作业:4/20
    网络编程基础(socket)
    网络基础之网络协议
    异常处理
    python 作业4/15
    centos7简单安装配置mariadb
    Centos7下Firewalld防火墙配置命令
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13266309.html
Copyright © 2011-2022 走看看