無聊的時候玩玩人人農場,不知不覺等級就高了,於是乎就招偷了……作為乙個玩農場的程式設計師,不能對此束手無策的!寫個收菜的程式吧,順便練練python!
研究了一下人人網的wap頁面,寫下了如下的**:
#! /usr/bin/env python
#coding=utf-8
import urllib2,urllib
from xml.dom import minidom
from htmlparser import htmlparser
login_url=''
def login(user, passwd):
data=urllib.urlencode()
fd=urllib2.urlopen(login_url,data)
return fd.read()
doc=minidom.parsestring(page)
url=a.getattribute('href')
return url
else:
print '沒有找到「應用」頁面的鏈結'.decode('utf-8').encode('gbk')
def getfarmurl(page):
doc=minidom.parsestring(page)
farmdiv=doc.getelementsbytagname('body')[0].getelementsbytagname('div')[5]
for a in farmdiv.getelementsbytagname('a'):
url=a.getattribute('href')
return url
else:
print '沒有找到「人人農場」頁面的鏈結'.decode('utf-8').encode('gbk')
def visiturl(url):
fd=urllib2.urlopen(url)
return fd.read()
def harvestfarm(user, passwd):
print '收穫'.decode('utf-8').encode('gbk')+user+\
'的農場……'.decode('utf-8').encode('gbk')
tp=targetsparser()
tp.feed(visiturl(farmurl))
linklists= tp.gettargets()
for url in linklists:
hlp=harvestlinkparser()
hlp.feed(visiturl(url))
if hlp.getharvestlink():
visiturl(hlp.getharvestlink())
print '收工離開此農場'.decode('utf-8').encode('gbk')
class targetsparser(htmlparser):
def __init__(self):
self.targets=
htmlparser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag!='a':
return
href=[value for key,value in attrs if key=='href']
for url in href:
if 'mycropaction.php' in url or 'mytreeaction.php' in url or\
'myanimalaction.php' in url or 'mymachineaction.php' in url:
def gettargets(self):
return self.targets
class harvestlinkparser(htmlparser):
def __init__(self):
self.link=''
htmlparser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag!='a':
return
href=[value for key,value in attrs if key=='href']
for url in href:
if 'wap,reapallaction.php' in url:
def getharvestlink(self):
return self.link
if __name__=='__main__':
userlist=[('email','password')]
for u,p in userlist:
harvestfarm(u,p)
經過測試,已經可以實現收菜功能了。接下來的任務是:定時收菜、自動定時收菜、收菜時順便偷菜……
嘿嘿,分享**僅作為技術交流~