#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = 'poppy' ''' dakele bbs sigin ''' import sys import urllib2 import urllib import requests import cookielib import json from pyquery import PyQuery as pq import logging logging.basicConfig(level=logging.DEBUG) reload(sys) sys.setdefaultencoding("utf8") class Dakele(object): def __init__(self,name,password): self.name = name self.password = password self.cj = cookielib.LWPCookieJar() self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) urllib2.install_opener(self.opener) def _getHeaders(self): headers = {} headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' #headers['Host']='www.dakele.com' headers['Connection']='keep-alive' headers['Cache-Control']='max-age=0' headers['Accept-Language']='zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4' #headers['Accept-Encoding']='gzip, deflate, sdch' headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' return headers def login(self): '''登录网站''' logging.debug(u'正在登陆 username : %s password : %s' %(self.name,self.password)) logging.debug(u'headers is : %s' % self._getHeaders()) loginparams = {'product': 'bbs','surl': r'http://bbs.dakele.com/','username': self.name,'password':self.password,'remember':'0'} logging.debug(u'loginparams is : %s' % loginparams) req = urllib2.Request( r'http://passport.dakele.com/logon.do', urllib.urlencode(loginparams), headers=self._getHeaders()) response = urllib2.urlopen(req) self.operate = self.opener.open(req) thePage = response.read() result = json.loads(thePage) return result['redirect'] def login_bbs(self,url): '''登录bbs网站''' logging.debug( 'start bbs login : %s ' % url) req = urllib2.Request(url,headers=self._getHeaders()