[size=small;] 其实原理很简单,首先用firfox的插件httpfox来获取登录、查看菜地和偷菜的http请求,再使用python的urllib来模拟发送http请求,对回应数据进行分析,就ok了。[/size]
[size=small;] 第一步:登录[/size]
[size=small;] 第二部:进入花园,获取verify代码[/size]
[size=small;] 第三步:查看有成熟菜的好友列表[/size]
[size=small;] 第四步:查看某个好友的菜地信息[/size]
[size=small;] 第五步:开偷[/size]
[size=small;] 代码如下:[/size]
[size=small;]
#-*- coding:utf-8 -*-import urllibimport socketimport http.cookiejar import randomimport timeimport sysclass farm: cj = http.cookiejar.cookiejar() is_login = false user = "" password = "" verify = "" steal_list = [] def __init__(self,user,password,steal_list): self.user = user self.password = password self.steal_list = steal_list.split(",") def login(self): url = "http://www.kaixin001.com/login/login.php"; opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(self.cj)) print("正在登陆开心网....") f = opener.open(url,"url=/home/&email=" + self.user + "&password=" + self.password) f.read() url = "http://www.kaixin001.com/!house/garden/index.php" print("正在进入农场....") f = opener.open(url) data = f.read() idx = str(data).find("var g_verify = \""); if idx == -1: return idx2 = str(data).find("\"",idx + 18) if idx2 == -1: return is_login = true self.verify = str(data)[idx+16:idx2] print("获取key=" + self.verify) def _havest_one(self,farm_num,fuid): if self.verify == "": print("用户尚未登录!") return opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(self.cj)) url = "http://www.kaixin001.com/!house/!garden/havest.php?farmnum=" + farm_num + "&seedid=0&fuid=" + fuid + "&r=" + str(random.uniform(0,1)) f = opener.open(url) data = str(f.read().decode("utf-8")) if not self.validate(data) : self.login() return index1 = data.find("<ret>") index2 = data.find("</ret>") result = data[index1+5:index2] if result == "succ": name = data[data.find("<seedname>")+10:data.find("</seedname>")] if fuid != "0": print("★★☆☆成功偷了id(" + fuid + ")家的" + name + "!★★☆☆") else: print("★★☆☆成功收获了自己的" + name + "!★★☆☆") else: index1 = data.find("<reason>") index2 = data.find("</reason>") reason = data[index1+8:index2] if fuid != "0": print("!!!!偷id(" + fuid + ")家的菜失败,失败原因:" + reason + "!") else: print("!!!!收获自己家的菜失败,失败原因:" + reason + "!") time.sleep(3) def validate(self,str): if str.find("您需要登录后才能继续浏览或操作") != -1: return false return true def havest(self,fuid): if self.verify == "": print("用户尚未登录!") return opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(self.cj)) url = "http://www.kaixin001.com/!house/!garden/getconf.php?verify=" + self.verify + "&fuid=" + fuid + "&r=" + str(random.uniform(0,1)) f = opener.open(url) data = f.read() result = str(data.decode("utf-8")) if not self.validate(result) : self.login() return index = result.find("<farmnum>") name = result[result.find("<name>")+6:result.find("</name>")] while index != -1: index2 = result.find("</farmnum>",index) num = result[index+9:index2] index3 = result.find("<cropsstatus>",index) index4 = result.find("</cropsstatus>",index) if index3 == -1 or index4 == -1: index = result.find("<farmnum>",index+10) continue status = result[index3+13:index4] status_str = "状态未知(" + status + ")"; if status == "1": status_str = "未成熟" if status == "2": status_str = "已成熟" if status == "3": status_str = "没东西" index3 = result.find("<name>",index) index4 = result.find("</name>",index) fname = "未知" if index3 != -1 and index4 != -1: fname = result[index3+6:index4] print(name + "的第" + num + "块地[" + fname + "]" + status_str) #成熟的 if status == "2": flag = false for f in self.steal_list: if f == fname : self._havest_one(num,fuid) flag = true if not flag: print("●●○○" + fname + "太便宜了,不偷!!!!") index = result.find("<farmnum>",index+10) def friend_list(self): opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(self.cj)) url = "http://www.kaixin001.com/!house/!garden/getfriendmature.php?verify=" + self.verify + "&r=" + str(random.uniform(0,1)) f = opener.open(url) data = f.read() result = str(data.decode("utf-8")) index = result.find("\"uid\":") while index != -1: index2 = result.find(",",index) uid = result[index+6:index2] index3 = result.find("\"realname\":",index) index4 = result.find(",",index3) if index3 == -1 or index4 == -1: index = result.find("\"uid\":",index+3) continue name = result[index3+11:index4] print("uid=" + uid + ",name=" + name) self.havest(uid) time.sleep(5) index = result.find("\"uid\":",index+3) f = farm(sys.argv[1],sys.argv[2],sys.argv[3])f.login()while true: f.havest("0") time.sleep(10) f.friend_list() time.sleep(10)
<font size="3">
</font>[/size]
[size=small;] [/size]
[size=small;]使用方法:[/size]
[size=small;]1、下载安装python3.1[/size]
[size=small;]2、保存上边的文本为farm.py[/size]
[size=small;]3、命令行下敲入[/size]
[size=small;];farm.py 开心网用户名 开心网密码 要偷的菜的列表(半角逗号分割)[/size]