例子,python/login/ target=_blank class=infotextkey>python模拟登录自动领取下载豆。
#!/usr/bin/python
#coding : utf-8
#site: www.jb200.com
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')