• 模拟登陆京东并访问我的订单


    第一个出现错误

    # -*- coding: utf-8 -*-
    
    import requests 
    
    url = 'https://passport.jd.com/uc/loginService'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
    
    data = {
        
        'loginname': 'XXXXXXX',
        'nloginpwd': 'XXXXXXXX'
    }
    
    r = requests.post(url, data)
    print r.text
    
    url1 = 'https://order.jd.com/center/list.action'
    
    r1 = requests.get(url1)
    print r1.text 

     第二个正常

    import requests 
    
    url = 'https://passport.jd.com/uc/loginService'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
    
    data = {
      
      'loginname': 'XXXXXXX',
      'nloginpwd': 'XXXXXXXX'
    }
    
    session = requests.Session()
    
    r = session.post(url, data)
    
    print r.text
    
    url1 = 'https://order.jd.com/center/list.action'
    
    r1 = session.get(url1)
    print r1.text 

    非常清晰体现出了session的用途.   

    requests库的session对象能够帮我们跨请求保持某些参数,也会在同一个session实例发出的所有请求之间保持cookies

    print r.cookies.values()
    print r1.cookies.values()
    
    print r.cookies.keys()
    print r1.cookies.keys()
    
    print r.cookies.get_dict()
    print r1.cookies.get_dict()

    示例 

    s = requests.session()
    # req_param = '{"belongId": "300001312","userName": "alitestss003","password":"pxkj88","captcha":"pxpx","captchaKey":"59675w1v8kdbpxv"}'
    # res = s.post('http://test.e.fanxiaojian.cn/metis-in-web/auth/login', json=json.loads(req_param))
    # # res1 = s.get("http://test.e.fanxiaojian.cn/eos--web/analysis/briefing")
    # print(res.cookies.values()) 获取登陆的所有session

    参考文献:

    http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  

    https://www.jb51.net/article/141305.htm

    https://www.cnblogs.com/lingxia/p/9074620.html 

  • 相关阅读:
    centos7 yum 安装mariadb
    curl 获取外网IP
    ansible之一:安装与配置
    第一步 django的下载安装
    命名空间的三种引用方式:非限定名称、限定名称、完全限定名称
    重温PHP之快速排序
    PHP实现文件下载的核心代码
    PHP常量定义之define与const对比
    ThinkPHP中I('post.')与create()方法的对比
    重温PHP之选择排序
  • 原文地址:https://www.cnblogs.com/huiming/p/9880288.html
Copyright © 2020-2023  润新知