关于使用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="")
posted on 2010-10-15 07:07  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/10/15/1851859.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值