zoukankan      html  css  js  c++  java
  • 关于使用cookielib的一些用法

    import socket as original_socket
    original_socket.setdefaulttimeout(10)
    from eventlet.timeout import with_timeout
    from eventlet.green import urllib2
    def login():
        import urllib
        import cookielib
        cj=cookielib.LWPCookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        params={"username":"mlzboy","password":"mlzboy","act":"act_login","back_act":"./index.php"}
        headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                 "Referer":"http://www.modengnvhai.com/category-165-b0.html"}
        req=urllib2.Request("http://www.modengnvhai.com/user.php",urllib.urlencode(params),headers=headers)
        operate=opener.open(req)
        if operate.geturl()=="http://www.modengnvhai.com/user.php":
            print "login success"
            req=urllib2.Request('http://www.modengnvhai.com/goods-1203.html',headers=headers)
            res=urllib2.urlopen(req)
            open("html.txt","w").write(res.read())
            #cj.save("cookie2.txt")
            cj.save("cookie.txt")
        else:
            print "login error"
    def get_html():
        import urllib
        import cookielib
        cj=cookielib.LWPCookieJar()
        try:
            cj.revert('cookie.txt')
        except Exception,e:
            print e
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                 "Referer":"http://www.modengnvhai.com/category-165-b0.html"}
        req=urllib2.Request('http://www.modengnvhai.com/goods-1203.html',headers=headers)
        res=urllib2.urlopen(req)
        open("html.txt","w").write(res.read())
        cj.save("cookie2.txt")
        print "done"
    def get_html3(url):
        def do(url):
            try:
                
                req = urllib2.Request(url = url,headers = headers)
                img = urllib2.urlopen(req)
                return img.url
            except Exception,ex:
                error(url,ex)
                return ""
        rr = with_timeout(10, do, url, timeout_value="")
    import socket as original_socket
    original_socket.setdefaulttimeout(10)
    from eventlet.timeout import with_timeout
    from eventlet.green import urllib2
    def login():
        import urllib
        import cookielib
        cj=cookielib.LWPCookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        params={"username":"mlzboy","password":"mlzboy","act":"act_login","back_act":"./index.php"}
        headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                 "Referer":"http://www.modengnvhai.com/category-165-b0.html"}
        req=urllib2.Request("http://www.modengnvhai.com/user.php",urllib.urlencode(params),headers=headers)
        operate=opener.open(req)
        if operate.geturl()=="http://www.modengnvhai.com/user.php":
            print "login success"
            cj.save("cookie.txt")
        else:
            print "login error"
    def get_html():
        import urllib
        import cookielib
        cj=cookielib.LWPCookieJar()
        try:
            cj.revert('cookie.txt')
        except Exception,e:
            print e
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                 "Referer":"http://www.modengnvhai.com/category-165-b0.html"}
        req=urllib2.Request('http://www.modengnvhai.com/goods-1203.html',headers=headers)
        res=urllib2.urlopen(req)
        open("html.txt","w").write(res.read())
        cj.save("cookie2.txt")
        print "done"
    def get_html3(url):
        def do(url):
            try:
                
                req = urllib2.Request(url = url,headers = headers)
                img = urllib2.urlopen(req)
                return img.url
            except Exception,ex:
                error(url,ex)
                return ""
        rr = with_timeout(10, do, url, timeout_value="")
  • 相关阅读:
    Next Permutation
    Substring with Concatenation of All Words
    Divide Two Integers
    Remove Duplicates from Sorted Array
    3sum closest
    ThreadPoolExecutor参数与拒绝策略
    多线程情况下ArrayList 如何解决线性安全问题
    ArrayList扩容机制jdk1.8
    SpringCloud--工作流程(好文)
    Java面试——TCP与HTTP
  • 原文地址:https://www.cnblogs.com/lexus/p/1851859.html
Copyright © 2011-2022 走看看