使用python在校內(nèi)發(fā)人人網(wǎng)狀態(tài)(人人網(wǎng)看狀態(tài))
#_*_coding:utf_8_
from sgmllib import SGMLParser
import sys, urllib2, urllib, cookielib
import datetime, time
class spider(SGMLParser):
def __init__(self, email, password):
SGMLParser.__init__(self)
self.email = email
self.password = password
self.domain = 'renren.com'
try:
cookie = cookielib.CookieJar()
# a class to handle HTTP cookies
cookieProc = urllib2.HTTPCookieProcessor(cookie)
except:
raise
else:
opener = urllib2.build_opener(cookieProc)
urllib2.install_opener(opener)
def login(self):
print '開始登錄'
url = 'http://www.renren.com/PLogin.do'
#url = 'http://www.renren.com/SysHome.do'
postdata = {
'email': self.email,
'password': self.password,
'domain': self.domain
}
# 一般情況下引入urllib2的地方都需要引入urllib,因?yàn)樾枰猽rlencode()
req = urllib2.Request(
url,
urllib.urlencode(postdata)
)
self.file = urllib2.urlopen(req).read()
# urlopen后 成功后進(jìn)入首頁 因此self.file的內(nèi)容就是首頁的html文件的內(nèi)容
# print self.file
idPos = self.file.index("'id':'")
self.id = self.file[idPos+6:idPos+15]
tokPos = self.file.index("get_check:'")
self.tok = self.file[tokPos+11:tokPos+21]
rtkPos = self.file.index("get_check_x:'")
self.rtk = self.file[rtkPos+13:rtkPos+21]
def publish(self, content):
url1 = 'http://shell.renren.com/' +self.id+ '/status'
print 'self.id = ' , self.id
postdata = {
'content': content,
'hostid': self.id,
'requestToken': self.tok,
'_rtk': self.rtk,
'channel': 'renren',
}
req1 = urllib2.Request(
url1,
urllib.urlencode(postdata)
)
self.file1 = urllib2.urlopen(req1).read()
print datetime.datetime.now()
print '剛才賬號 %s發(fā)了一條狀態(tài)' % self.email
print '內(nèi)容為: %s' % postdata.get('content', '')
renrenspider = spider('qich555550@163.com', 'qishibo123')
renrenspider.login()
#content = raw_input('請輸入狀態(tài)的內(nèi)容:')
contents =["祝","各","位","同","學(xué)","盆","友","在","新","的","一","年","里","身","體","健","康","萬","事","如","意","不小心刷屏了,望大家諒解"]
#renrenspider.publish(content)
#content = "新年快樂"
#renrenspider.publish(content)
#renrenspider.publish(content.decode('gb2312').encode('utf-8'))
for content in contents:
renrenspider.publish(content)
用這個(gè)程序就可以發(fā)狀態(tài)刷屏了,只不過校內(nèi)的狀態(tài)不支持具體時(shí)間,看不出效果來,不然每隔兩秒一條狀態(tài)應(yīng)該會讓人驚訝的
相關(guān)文章
python opencv檢測直線 cv2.HoughLinesP的實(shí)現(xiàn)
cv2.HoughLines()函數(shù)是在二值圖像中查找直線,本文結(jié)合示例詳細(xì)的介紹了cv2.HoughLinesP的用法,感興趣的可以了解一下2021-06-06使用python將時(shí)間轉(zhuǎn)換為指定的格式方法
今天小編就為大家分享一篇使用python將時(shí)間轉(zhuǎn)換為指定的格式方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python首次安裝后運(yùn)行報(bào)錯(0xc000007b)的解決方法
最近在安裝完P(guān)ython后運(yùn)行發(fā)現(xiàn)居然報(bào)錯了,錯誤代碼是0xc000007b,于是通過往上查找發(fā)現(xiàn)是因?yàn)槭状伟惭bPython缺乏VC++庫的原因,下面通過這篇文章看看如何解決這個(gè)問題吧。2016-10-10Python機(jī)器學(xué)習(xí)入門(五)之Python算法審查
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)入門知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08