1 #encoding:utf-8
2 import urllib,urllib2,cookielib,re,time
3 class renren():
4 def __init__(self,email,password):
5 self.url='http://www.renren.com/'
6 self.email=email
7 self.password=password
8 self.cookie=cookielib.LWPCookieJar()
9 self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
10 self.friendIdList=[]
11 def login(self):
12 url=self.url+'PLogin.do/'
13 params=urllib.urlencode({'email':self.email,'password':self.password})
14 response=self.opener.open(urllib2.Request(url,params))
15 url=self.url+'home'
16 if response.geturl()==url:
17 print 'Login Succesfully!'
18 self.cookie.save('xiaonei.coockie')
19 else:
20 print 'Login Failed'
21
22 def changeState(self):
23 pass
24
25 def getFriendList(self):
26 url='http://friend.renren.com/myfriendlistx.do'
27 response=self.opener.open(urllib2.Request(url))
28 html=response.read()
29 friend=str(re.search('friends=\[{.*}\]',html).group())
30 friendId=re.findall('"id":\d+',friend)
31 for id in friendId:
32 self.friendIdList.append(id.lstrip('"id":'))
33
34 def browseFriend(self):
35 cnt=0
36 for friendId in self.friendIdList:
37 cnt+=1
38 url='http://www.renren.com/profile.do?id='+'%s'%(friendId)
39 urllib2.Request(url)
40 time.sleep(1)
43 if __name__=='__main__':
44 app=renren('your email','your password')
45 app.login()
46 app.changeState()
47 app.getFriendList()
48 app.browseFriend()
49
50
changeState 因為不知道怎么分析校內的表單提交方式,他的源碼沒法查看出表單時提交到哪里了,所以不好寫,而類似getFriendList,訪問好友啊這樣的操作則是可以的,同樣還可以寫日志啊什么的。
有誰能告訴我怎么分析校內這種隱藏的表單
<input type="submit" autocomplete="false" id="publisher_submit" class="submit" value="發布" stats="Pub_Send">
是后臺的js代碼么
不過話說回來,python的urllib,urllib2什么的倒是很好用,做一些相關的應用都是很簡單的