[转]python cookielib
http://blog.163.com/longsu2010@yeah/blog/static/173612348201161531958610/
今天晚上不是很忙,所以早早的就在电脑的旁边开始写东西了。我今天给大家分享一个我自己用python写的自动登录 人人网的脚本,没办法就是懒!懒的输入帐号和密码,让python给我们减少工作量! 先上脚本吧,等下来讲下知识点:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/env python 2 #encoding=utf-8 3 import sys 4 import re 5 import urllib2 6 import urllib 7 import cookielib 8 9 class Renren(object): 10 11 def __init__(self): 12 self.name = self.pwd = self.content = self.domain = self.origURL = '' 13 self.operate = ''#登录进去的操作对象 14 self.cj = cookielib.LWPCookieJar() 15 try: 16 self.cj.revert('renren.coockie') 17 except Exception,e: 18 print e 19 20 self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 21 urllib2.install_opener(self.opener) 22 23 24 def setinfo(self,username,password,domain,origURL): 25 '''设置用户登录信息''' 26 self.name = username 27 self.pwd = password 28 self.domain = domain 29 self.origURL = origURL 30 31 def login(self): 32 '''登录人人网''' 33 params = {'domain':self.domain,'origURL':self.origURL,'email':self.name,'password':self.pwd} 34 print 'login.......' 35 req = urllib2.Request( 36 'http://www.renren.com/PLogin.do', 37 urllib.urlencode(params) 38 ) 39 40 self.operate = self.opener.open(req) 41 42 if self.operate.geturl() == 'http://www.renren.com/Home.do': 43 print 'Logged on successfully!' 44 self.cj.save('renren.coockie') 45 self.__viewnewinfo() 46 else: 47 print 'Logged on error' 48 49 def __viewnewinfo(self): 50 '''查看好友的更新状态''' 51 self.__caiinfo() 52 53 54 def __caiinfo(self): 55 '''采集信息''' 56 57 h3patten = re.compile('<h3>(.*?)</h3>')#匹配范围 58 apatten = re.compile('<a.+>(.+)</a>:')#匹配作者 59 cpatten = re.compile('</a>(.+)\s')#匹配内容 60 infocontent = self.operate.readlines() 61 # print infocontent 62 print 'friend newinfo:' 63 for i in infocontent: 64 content = h3patten.findall(i) 65 if len(content) != 0: 66 for m in content: 67 username = apatten.findall(m) 68 info = cpatten.findall(m) 69 if len(username) !=0: 70 print username[0],'说:',info[0] 71 print '----------------------------------------------' 72 else: 73 continue 74 75 ren = Renren() 76 username = ''#你的人人网的帐号 77 password = ''#你的人人网的密码 78 domain = 'renren.com'#人人网的地址 79 origURL = 'http://www.renren.com/Home.do'#人人网登录以后的地址 80 ren.setinfo(username,password,domain,origURL) 81 ren.login()
主要用到了python cookielib,urllib2,urllib这3个模块,这3个模块是python做http这方面比较好的模块.
self.cj = cookielib.LWPCookieJar()
try:
self.cj.revert('renren.coockie')
except Exception,e:
print e
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
try:
self.cj.revert('renren.coockie')
except Exception,e:
print e
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
这几行是在本地建立人人网的cookies,因为人人网要验证cookies才能登录,你运行这个脚本的话,会发现在当前目录 有个程序会自动建立一个renren.cookie这个文件。
我这里renren.cookie的信息是: #LWP-Cookies-2.0 Set-Cookie3: WebOnLineNotice_244225225=1; path="/"; domain=".renren.com"; path_spec; domain_dot; expires="2010-04-11 06:59:33Z"; version=0 总结一下如果网站登录要用cookie的话,就要用到cookielib这个模块,不然你用程序登录不了网站,过断时间在写个urlib的例子,大家可以先用上面这个脚本玩玩!体会下python的乐趣!
作者:老王@python python教程 老王python,提供pythn相关的python教程和python下载,希望大家能够喜欢