• 关于使用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="")
  • 相关阅读:
    使用python对mysql主从进行监控,并调用钉钉发送报警信息
    CentOS7下安装gitlab
    nginx日志自动切割
    Mysql定时备份数据脚本
    Linux下搭建FTP服务
    linux系统盘使用率达到100%的问题查找和解决方法
    CentOS6.5+nginx+mysql+php(laravel)服务器环境搭建
    RHEL6和RHEL7恢复root用户密码
    在Dell R720服务器上安装ESXI5.5时会出现卡在LSI_MR3.V00的解决方法
    /23 /24 /26/28 /29 /30或10.0.0.1/29这样怎么算服务器IP数是多少?
  • 原文地址:https://www.cnblogs.com/lexus/p/1851859.html
Copyright © 2020-2023  润新知