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

发布时间:2020-03-01编辑:脚本学堂
本文介绍了python模拟登录的方法,python使用cookie自动领取下载豆,有关python模块urllib2、cookielib、urllib实现模拟登录的实例教程,需要的朋友参考下。

例子,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')