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="")
  • 相关阅读:
    第十五周总结
    第二阶段冲刺
    课堂练习
    第十四周总结
    WP8 NavigationInTransition实现页面切换效果
    WP8学习笔记:如何在页面显示前自动转向到其他页面
    WP8简单的计算器
    WP8滑动条(Slider)控件的使用
    Wp8滚动区域(ScrollViewer)控件的使用
    C#中ToString格式大全
  • 原文地址:https://www.cnblogs.com/lexus/p/1851859.html
Copyright © 2011-2022 走看看