python爬虫51cto领下载豆和无忧币

#coding:UTF-8

import urllib,urllib2,cookielib,re,random

class Login:
    _login_url = 'http://home.51cto.com/index.php?s=/Index/doLogin'
    _method = 'post'
    #email 51cto登录用户名或邮箱
    #passwd 51cto登录密码
    _login_data = {
                   'email':'herb',\
                   'passwd':'herb',\
            }
    _headers = [
                ('host','home.51cto.com'),\
                ('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'),\
                ('Referer','http://home.51cto.com/index.php?s=/Index/index/reback/http%253A%252F%252Fwww.51cto.com%252F/')\
        ]
    _data = {
             'cookie_file_path':'./51cto_cookies.dat'
        }
    _re = r'src="(.+?)"'
    _version = '0.1'
    _connect_info = {}
    def __init__(self):
        self._connect_info['cookie'] = cookielib.LWPCookieJar()
        try:
            self._connect_info['cookie'].revert(self._data['cookie_file_path'])
        except Exception,e:            
            print e
        self._connect_info['cookie_processor'] = urllib2.HTTPCookieProcessor(self._connect_info['cookie'])
        self._connect_info['post_data'] = urllib.urlencode(self._login_data)
    def open(self):
        opener = urllib2.build_opener(self._connect_info['cookie_processor'])
        opener.addheaders = self._headers
        urllib2.install_opener(opener)

       
        #opener.open(request)
        request = urllib2.Request(self._login_url,self._connect_info['post_data'])
        conn = opener.open(request)
        if(conn.geturl() == self._login_url):
            self._connect_info['cookie'].save(self._data['cookie_file_path'])
        else:
            pass
        #根据js中的链接连接登录
        partner = re.compile(self._re)
        match = partner.findall(conn.read())
       
        for item in match:
            opener.open(item)
             
       
        #登录成功开始领豆
        url = 'http://down.51cto.com/download.php'
        data = {'do':'getfreecredits','t':random.random()}
        opener.open(url, urllib.urlencode(data))
        #html = opener.open('http://down.51cto.com/')
       
        #领无忧币
        url = 'http://home.51cto.com/index.php?s=/Home/toSign'
        data = {'s':''}
        opener.open(url, urllib.urlencode(data))

if __name__ == '__main__':
    login_51cto = Login()
    login_51cto.open()
       
       
  当前目录先建立空文件 51cto_cookies.dat ,然后执行脚本
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值