• 使用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())
  • 相关阅读:
    Repository模式介绍汇总
    EasyUI实例源码
    java callable
    Java线程:Callable和Future
    guava cache学习
    ESG操作指南
    spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on
    sss
    一对多多对多的建表原则
    mybatis collection标签和association标签(一对多,一对一)转载
  • 原文地址:https://www.cnblogs.com/jestin/p/12911375.html
Copyright © 2020-2023  润新知