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="")
  • 相关阅读:
    poj 3528 (三维几何求凸包+凸包表面积)
    dijkstra模板(好像是斐波那契额堆优化,但我为什么看起来像优先队列优化,和spfa一样)
    最大空凸包模板
    ICPC 2017–2018, NEERC, Northern Subregional Contest St Petersburg, November 4, 2017 I题
    hdu 5248 序列变换
    hdu 2063(二分图模板测试)
    组合数
    85. Maximal Rectangle 由1拼出的最大矩形
    750. Number Of Corner Rectangles四周是点的矩形个数
    801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数
  • 原文地址:https://www.cnblogs.com/lexus/p/1851859.html
Copyright © 2011-2022 走看看