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())
  • 相关阅读:
    NDOC中文支持及入门用法
    网页代码常用小技巧
    SOCKET通讯点滴
    自动备份程序目录
    MySql.Data.dll Microsoft.Web.UI.WebControls.dll下载
    c#:获取IE地址栏中的URL
    比较好的单例登录模式(参考网友)
    FreeTextBox使用详解
    2005自定义控件显示基准线
    连接字符串大全
  • 原文地址:https://www.cnblogs.com/jestin/p/12911375.html
Copyright © 2011-2022 走看看