python登录网页的两种方式

发布时间:2019-09-04 06:54:50编辑:auto阅读(1704)

    以登录我校系统为例:
    1. 观察传输的包,填写内容登录

    #_*_ coding:utf-8 _*_
    import requests
    
    url = 'https://cas.xjtu.edu.cn/login'
    header = { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'
    }
    #这是我从chrome f12里点登录后看到的传输的内容
    from_data = {'username': 'xxx',
        'password': 'xxx',
        'code': '',
        'lt': 'xxx',
        'execution': 'e1s1',
        '_eventId': 'submit',
        'submit': '登录'
    }
    s = requests.session()
    response = s.post(url, data = from_data, headers = header)
    print response.status_code
    
    

    2.利用cookie直接上

    #_*_ coding:utf-8 _*_
    import requests
    
    url = 'https://cas.xjtu.edu.cn/login;jsessionid=084E35D36A4DA796BBB9A12E5E221BBE?service=http://my.xjtu.edu.cn/Login'
    header = { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36',
        'Cookie': 'xxx'
    }
    
    s = requests.session()
    response = s.post(url, headers = header)
    print response.status_code 

关键字