注册为新员工,还没转正,挂了几天发现kx没见涨,后听说要不停的刷新才有效,昨写了个py脚本,挂了1.5小时,今天终于看到涨了1kx。
与临时会员分享。如内容有侵犯论坛利益和版规,请斑竹告知,并删除。谢谢~~
任何人不得作为恶意目的使用,如违反相关规定,由使用者承担责任,与本人无关。
# -*- coding: utf-8 -*-
# main.py
import urllib2,httplib
import cookielib
import socket
import gzip
import StringIO
import urllib
import md5
import time
import random
import sys
#######################################################################
# 参数输入
# 登录用户名
user = "yourname"
# 登录密码
passwd = "yourpasswd"
# 连续访问时长秒,建议大于1小时,我这里设置的是1.2小时
onlineTotal = 1.2 * 60 * 60
# 刷新间隔,建议1分钟左右,防止对服务器照成压力。我这里用的是1分钟
interval = 60
# 用法,命令行下:
# python main.py
#######################################################################
g_cookie = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
def _UrlOpen(cj, url, headers, postData, flag="POST"):
global g_cookie
opener=urllib2.build_opener(cj)
request=urllib2.Request(url)
for key, val in headers.items():
request.add_header(key, val)
if "Accept-Encoding" in headers:
request.add_header("Accept-Encoding", "gzip, deflate")
bincontent = None
try:
if postData:
connect = opener.open(request, postData)
elif flag == "POST":
connect = opener.open(request, postData)
else:
connect = opener.open(request)
isGzip = connect.headers.get('Content-Encoding')
if isGzip :
compresseddata = connect.read()
compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
bincontent = gzipper.read()
else:
bincontent = connect.read()
except httplib.BadStatusLine, e:
print "httplib.BadStatusLine", str(e)
except urllib2.URLError, e:
print "urllib2.URLError", str(e)
except socket.timeout,e:
print "socket.timeout", str(e)
except KeyboardInterrupt:
print "KeyboardInterrupt"
except :
print "error <-------------------"
finally:
pass
return bincontent
def AccessPage(url):
print "%s访问[%s]"%(time.strftime("%Y-%m-%d %H:%M:%S"), url)
# url = "http://bbs.pediy.com/"
headers = {}
content = _UrlOpen(g_cookie, url, {}, "", "GET")
# f =open("Access.txt",'wb')
# f.write(content)
# f.close()
def LoginSite(username, password):
print "%s尝试登录到服务器"%time.strftime("%Y-%m-%d %H:%M:%S")
url = 'http://bbs.pediy.com/login.php'
_username_itf8 = username.decode('gb2312').encode('utf8')
_username = urllib.quote(_username_itf8)
md5obj = md5.md5()
md5obj.update(password)
_password = md5obj.hexdigest()
postDict = {
"vb_login_username" : _username,
"cookieuser" : "1",
"vb_login_password" : "",
"s" : "",
"securitytoken" : "guest",
"do" : "login",
"vb_login_md5password" : _password,
"vb_login_md5password_utf" : _password,
}
postData = urllib.urlencode(postDict)
content = _UrlOpen(g_cookie, url, {}, postData, "POST")
# 登录过程中连接失败
if content == None:
return False
# 感谢用户登录 标志登录成功.
if _username_itf8 in content and '感谢您登录'.decode('gb2312').encode('utf8') in content:
print "登录成功"
return True
else:
print "登录失败"
# f =open("login fail.txt",'wb')
# f.write(content)
# f.close()
return False
def SleepAndPrint(len):
while len > 0:
sys.stdout.write("等待[%d]秒\r"%len)
time.sleep(1)
len -= 1
def HelloWorld():
global user,passwd,onlineTotal,interval
# setup 1 访问 http://bbs.pediy.com 获取cookie
url = "http://bbs.pediy.com/"
AccessPage(url)
# setup 1 登录
LoginSite(user, passwd)
# setup 3 循环间隔访问
url = "http://bbs.pediy.com/index.php"
access = 0 # 访问次数
start = time.time()
while time.time() < start + onlineTotal:
AccessPage(url)
access += 1
sleeptime = interval + random.randint(-5, 5)
SleepAndPrint(sleeptime)
#print "睡眠[%d]秒"%sleeptime
#time.sleep(sleeptime)
# 结束打印
print "共访问[%d]次,现在结束"%access
if __name__=="__main__":
HelloWorld()
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!