龙空技术网

每天学一点,一个简单的python刷新浪微博粉丝小程序(附源码)

Python乐园 783

前言:

今天咱们对“cookielibpython3”大概比较着重,你们都需要了解一些“cookielibpython3”的相关内容。那么小编也在网上网罗了一些对于“cookielibpython3””的相关文章,希望同学们能喜欢,大家快快来了解一下吧!

代码简陋没有什么技术,还有个txt的配置文件才可以用。但是配置文件不发出来了。只要你自己好好看代码很容易猜得到。希望大家不要拿去刷了。因为实在是没意思。

代码中需要用到的相关python模块方法有: urllib, urllib2, cookielib, hashlib,threading,re, os, time, random,真的需要不少方法呢。

# -*- coding: utf-8 -*-

import urllib, urllib2, cookielib, hashlib,threading

import re, os, time, random

import sys

reload(sys)

sys.setdefaultencoding('utf8')

def log(s):

s = "\n" + str(s)

f = open('log.txt', 'a+')

f.write(s)

f.close()

sys.stdout.write(s)

sys.stdout.flush()

class WeiboCn:

all = ('玩蛇网python学习平台关注你啦~~~~','果断关注~','不知道你有没有发现你的粉丝多了一个?,哈哈!')

url = ''

header = {

'User-Agent' : 'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',

}

def __init__(self, username, password, keyword = None, *args):

self.user = username

self.keyword = keyword

self.all = args or self.all

self.cj = cookielib.LWPCookieJar()

self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))

urllib2.install_opener(self.opener)

self.tryLogin(username, password)

def tryLogin(self, username, password):

bodies = dict(_=int(time.time()),callback='sinaSSOController.preloginCallBack',client='ssologin.js(v1.3.12)',entry='miniblog',user='jiangjia@1616.net')

print "预登录,获取servertime & nonce参数(用来加密生成密码)"

preloadurl = '?' + urllib.urlencode(bodies)

content = self._request(preloadurl)[1].read()

bodies = eval(re.findall('\{.*?\}',content)[0])

password = hashlib.sha1(hashlib.sha1(hashlib.sha1(password).hexdigest()).hexdigest() + str(bodies['servertime']) + bodies['nonce']).hexdigest()

print "加密获得密码<%s>" % password

bodies.update(dict(client='ssologin.js(v1.3.12)',encoding='utf-8',entry='miniblog',gateway='1',password=password,pwencode='wsse',returntype='META',savestate='7',service='miniblog',ssosimplelogin='1',url=';callback=parent.sinaSSOController.feedBackUrlCallBack',username=username,useticket=1))

response = self._request('', bodies)[1]

content = response.read()

moreurl = re.findall('replace\([\'|"](.*?)[\'|"]\)', content)

if len(moreurl) == 0: print "登录失败!"

content = self._request(moreurl[0], dict(Referer='',Host='weibo.com'))[1].read()

if username in content:

print "登录成功!"

self.afterLogin()

def afterLogin(self):

content = self._request('')[1].read()

self.uid = re.findall('\$uid.*?"(\d+)"', content)[0]

def care(self):

url = ''

if self.keyword:

url = '' % urllib.quote(urllib.quote(self.keyword))

log(url)

content = self._request(url)[1].read()

match = re.findall('loadCommentByRid\((.*?)\)', content)

for x in match:

ownerUid,productId,productName,resourceId,resTitle,resInfo,some,listInDiv,forward,some2 = eval("(%s)" % x)

bodies = dict(uid=ownerUid,fromuid=self.uid,refer_sort='profile',atnId='profile')

result = self._request(';atnId=profile&rnd=%f' % random.random(), bodies, dict(Referer = ''))[1].read()

if 'A00006' in result:

log("<%s>关注<%d>成功!" % (self.user,ownerUid))

content = self.all[random.randint(0, len(self.all)-1)]

bodies = dict(content=content,uid=self.uid,ownerUid=ownerUid,productId=productId,productName=productName,resourceId=resourceId,resTitle=resTitle,resInfo=resInfo,some=some,listInDiv=listInDiv,forward=forward,some2=some2)

result = self._request(';rnd=%f' % random.random(), bodies, dict(Referer=''))[1].read()

if 'A00006' in result:

log("<%s>评论<%d>成功!<%s>" % (self.user,ownerUid, content))

else:

log("<%s>评论<%d>失败!" % (self.user,ownerUid))

time.sleep(random.randint(30, 4 * 60))

else:

log("<%s>关注<%d>失败!" % (self.user,ownerUid))

raise Exception,"关注<%d>失败!" % ownerUid

def unCare(self):

content = self._request('' % str(self.uid))[1].read()

pages = re.findall('<em>(\d+)<\/em>', content)

log(pages)

if len(pages) == 0:

return

p = apply(max,[int(i) for i in pages])

for i in range(p,0,-1):

log(i)

c

1fff

ontent = self._request(';tag=&page=%d' % i)[1].read()

cancel = re.findall('粉丝<strong>(.*?)</strong>.*?followcancel\(\'(\d+)', content, re.S)

headers = {'Content-Type':'application/x-www-form-urlencoded', 'Origin':'','Referer':';tag=&page=%d' % i}

url = '' % random.random()

for fans,id in cancel:

if int(fans) > 2000:

log("取消关注%s失败,原因:粉丝数%s大于2000" % (id, fans))

continue

result = self._request(url, dict(touid=id,fromuid=self.uid),headers)[1].read()

if 'A00006' in result:

log("取消关注%s成功!" % id)

else:

log("取消关注%s失败!" % id)

time.sleep(10)

def _request(self, url, bodies = {}, headers = {}):

request = urllib2.Request(url, urllib.urlencode(bodies), headers = headers)

return (request, self.opener.open(request))

def _readMainPage(self):

return self._request(self.url)[1].read()

class timer(threading.Thread):

def __init__(self, weibo):

threading.Thread.__init__(self)

self.weibo = weibo

def run(self):

#log (">>>>>>当前用户<%s>线程<%s>" % (self.weibo.user,self.getName()))

#t = int(time.strftime('%H'))

#looptime = 6 * 6 if t < 22 and t > 8 else 60 * 60 * 5

'''

try:

self.weibo.care()

log ("当前用户<%s>线程<%s>关注完一页, 休息%d小时,稍后继续~~" % (self.weibo.user,self.getName(), looptime/3600))

except Exception, e:

log(str(e))

log ("当前用户<%s>线程<%s>出现异常,可能是关注超过上限, 休息%d小时,稍后继续尝试~~" % (self.weibo.user,self.getName(), looptime/3600))

'''

self.weibo.unCare()

#time.sleep(looptime)

#self.run()

def run(userinc):

f = open(userinc).readlines()

if len(f) < 1 : log("配置文件为空!")

m = re.compile('\|')

userlist = []

for line in f:

if line.startswith('#'):continue

line = m.split(line.strip())

userlist.append(line)

allWeibo = [apply(WeiboCn,u) for u in userlist]

pound = []

for w in allWeibo:

w = timer(w)

w.setDaemon(True)

w.start()

pound.append(w)

#time.sleep(10)

for x in pound:

x.join()

if __name__ == '__main__':

run(os.path.join(os.path.dirname(__file__), 'user.txt'))

'''

while True:

m.runtimes += 1

m.run()

'''

标签: #cookielibpython3