zoukankan      html  css  js  c++  java
  • 使用cookie登录

    import urllib3
    import urllib
    import http.cookiejar
    import webbrowser
    #声明一个CookieJar对象实例来保存cookie
    #利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
    #此处的open方法同urllib2的urlopen方法,也可以传入request
    cookie = http.cookiejar.CookieJar()
    hander=urllib.request.HTTPCookieProcessor(cookie)
    opener=urllib.request.build_opener(hander)
    response=opener.open('http://www.baidu.com')
    for item in cookie:
        print( 'Name = '+item.name)
        print( 'Value = '+item.value)
    
    filename='cookie.txt'
    cookie=http.cookiejar.MozillaCookieJar(filename)
    hander=urllib.request.HTTPCookieProcessor(cookie)
    opener=urllib.request.build_opener(hander)
    response=opener.open('http://www.baidu.com')
    cookie.save(ignore_discard=True,ignore_expires=True)
    
    
    cookie=http.cookiejar.MozillaCookieJar()
    cookie.load('cookie.txt',ignore_discard=True,ignore_expires=True)
    req=urllib.request.Request('http://www.baidu.com')
    opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
    response=opener.open(req)
    print(response.read())
    
    
    from urllib import request
    if __name__ == '__main__':
        url = "http://oajy.sztljt.com:8080/sys/portal/page.jsp"
    
        headers = {
             #Cookie值从登录后的浏览器,拷贝,方法文章上面有介绍
            "Cookie": "zVEQTk1Q0I0NURBOTc4RDRjaGVuZ3hmR2DpSJ3lagbugXAfR/sqQONxXMk="
            }
    
        req = request.Request(url=url,headers=headers)
    
        rsp = request.urlopen(req)
        html = rsp.read().decode()
    
        with open("rsp.html","w",encoding="utf-8")as f:
             #将爬取的页面
            print(html)
            f.write(html)
    #模拟登录
    postdata=data.encode('utf-8')
    loginurl='http://oajy.com:8080/sys/portal/page.jsp'
    #获取cookie
    #保存到txt
    #读取cookie
    filename='OAcookie.txt'
    cookie=http.cookiejar.MozillaCookieJar(filename)
    hander=urllib.request.HTTPCookieProcessor(cookie)
    opener=urllib.request.build_opener(hander)
    result=opener.open(loginurl,postdata)
    cookie.save(ignore_discard=True,ignore_expires=True)
    ##登录网站
    geturl='http://zs.sztljyjt.com/admission/admission-manage'
    try:
        result=opener.open(geturl)
    except :
        print("登录失败")
    print(result.read())
  • 相关阅读:
    MySQL--CREATE INDEX在各版本的优化
    MySQL--各版本DDL 操作总结
    MySQL--事务隔离级别RR和RC的异同
    MySQL--运维内参中的binlog_summary脚本
    认知:人性
    诉衷情
    初中生读物
    DTO和Entity转换
    layui开发常用插件列表
    mongodb配置
  • 原文地址:https://www.cnblogs.com/jestin/p/12911375.html
Copyright © 2011-2022 走看看