python 模拟登录,使用cookie自动领取下载豆

#coding : utf-8
#51cto自动领豆
import urllib2
import cookielib
import urllib
import random
import re
def freedown(username,passwd):
    header = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1'),('Referer','http://home.51cto.com/index.php?s=/Index/index/reback/http%253A%252F%252Fdown.51cto.com')]
    cj = cookielib.LWPCookieJar()
    cookie_suppot = urllib2.HTTPCookieProcessor(cj)
    opener = urllib2.build_opener(cookie_suppot,urllib2.HTTPHandler)
    opener.addheaders = header
    urllib2.install_opener(opener)
    postdata = {'email': username,
                'passwd': passwd,
                'reback':'http%3A%2F%2Fdown.51cto.com',
                }
    request = urllib2.Request("http://home.51cto.com/index.php?s=/Index/doLogin",urllib.u    rlencode(postdata))
    conn = opener.open(request)
    partner = re.compile(r'src="(.+?)"')
    match = partner.findall(conn.read())
    for item in match:
        print item
        opener.open(item)
    url = 'http://down.51cto.com/download.php'
    data = {'do':'getfreecredits','t':random.random()}
    req = urllib2.Request(url,data)
    r = opener.open(req,urllib.urlencode(data))
    print r.read()
if __name__ == "__main__":
    freedown('username','password')

 

    

你可能感兴趣的:(python,自动,下载豆)