好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

关于使用cookielib的一些用法

关于使用cookielib的一些用法

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="")

查看更多关于关于使用cookielib的一些用法的详细内容...

  阅读:37次