原文地址:http://share.renren.com/share/GetShare.do?id=4175410964&owner=262195161&ref=minifeed&sfet=107&fin=1&ff_id=262195161
心血来潮写了个这么一个东西,就当是为py练练手了。最不习惯的地方就是冒号……
目前就是这样,核心代码其实就20多行,用于发状态
!!!不讲任何编码规范!!!不讲任何py习俗!!!不讲任何简单方法!!!不讲任何复杂测试!!!
-----------------poststatus.py-------------
# coding=utf-8
import urllib
import urllib2
import cookielib
import sys
xn={}
xn['email']='你懂的'
xn['password']='你也懂的'
cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
zhanghao = urllib.urlencode(xn)
req=urllib2.Request('http://3g.renren.com/login.do?fx=0&autoLogin=true',zhanghao)
resp=urllib2.urlopen(req)
#print resp.read()
def zhuangtai(sss):
xn2={}
xn2['status']=sss
xn2['update']="发布"
zhuangtai=urllib.urlencode(xn2)
req2=urllib2.Request('http://3g.renren.com/status/wUpdateStatus.do',zhuangtai)
print sss
resp2=urllib2.urlopen(req2)
之所以用3g而不是www的原因前文已经讲了。
-------------renren.py----------------主程序--------------------
#!/usr/bin/python
# coding=utf-8
import poststatus
import time
import hourstatus
import thread
import getstatus
#poststatus.zhuangtai('test')
def hoursleep():
t=time.localtime()
#print t[4]
if t[4]==59:
h=t[3]
if h==23:
h=0
else:
h+=1
poststatus.zhuangtai('下面是机器人整点报时时间:'+hourstatus.s[t[3]])
time.sleep(60)
hoursleep()
def rtsleep():
getstatus.zhuanfa()
time.sleep(60)
rtsleep()
print '欢迎打开您的机器人,您可以随时输入命令。exit退出,post发任意状态'
thread.start_new_thread(hoursleep,())
thread.start_new_thread(rtsleep,())
print '自动报时模块已加载'
while 1==1:
stdin=raw_input('shell:')
if stdin=='exit':
exit()
if stdin=='post':
while stdin!='robot' and stdin!='man':
stdin=raw_input('谁在说话?(robot/man)')
if stdin=='robot':
stdin=raw_input('我想说什么?')
poststatus.zhuangtai(stdin)
else:
stdin=raw_input('您想说嘛?')
poststatus.zhuangtai('我主人说:'+stdin)
continue
直接运行就可以
-----------------------getstatus.py-----------------------------
# coding=utf-8
import urllib
import urllib2
import cookielib
import sys
import poststatus
import time
def zhuanfa():
xn={}
xn['email']='你懂的'
xn['password']='你也懂的'
cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
zhanghao = urllib.urlencode(xn)
req=urllib2.Request('http://3g.renren.com/login.do?fx=0&autoLogin=true',zhanghao)
resp=urllib2.urlopen(req)
html=resp.read()
#print len(html)
index=html.find('转自')
while index>0:
html=html[index:]
index2=html.find('<p')
sss=html[:index2-1]
fw=open('zhuanfa.db','a')
fr=open('zhuanfa.db','r')
alllines=fr.readlines()
fr.close()
boo=1
for each in alllines:
if sss.find(each[len(each)/2:len(each)/3*2])>0:
boo=0
if boo>0:
poststatus.zhuangtai('自动转发:'+sss)
fw.write('\n'+sss+'\n')
else:
print '发现转发过的状态'
index=html.find('转自')
原理很简单,就是扫描一遍手机人人主页的第一页,只要是有“转自”就截取下来,跟存储的文件比对,如果没有转发过就转出去并存档。不存档的后果是显而易见的。
还有一个hourstatus.py,里面就是存了24个整点报时机器人要说的话,没别的
====================================
接下来的计划是写成真正的爬虫,能深入搜索什么的。可以建立一个好友的关系网什么的。那是以后的事情了。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com