这篇文章主要介绍了python实现自动登录人人网并采集信息的方法,涉及Python模拟登陆及正则匹配的相关技巧,需要的朋友可以参考下 本文实例讲述了python实现自动登录人人网并采集信息的

这篇文章主要介绍了python实现自动登录人人网采集信息的方法,涉及Python模拟登陆及正则匹配的相关技巧,需要的朋友可以参考下

python实现自动登录人人网并采集信息的方法  python 自动登录 人人网 采集 脚本之家 第1张

本文实例讲述了python实现自动登录人人网并采集信息的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. #!/usr/bin/python 
  2. # -*- coding: utf-8 -*- 
  3. import sys 
  4. import re 
  5. import urllib2 
  6. import urllib 
  7. import cookielib 
  8. class Renren(object): 
  9. def __init__(self): 
  10. self.name = self.pwd = self.content = self.domain = self.origURL = '' 
  11. self.operate = ''#登录进去的操作对象 
  12. self.cj = cookielib.LWPCookieJar() 
  13. try:  
  14. self.cj.revert('./renren.coockie')  
  15. except Exception,e: 
  16. print e 
  17. self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 
  18. urllib2.install_opener(self.opener) 
  19. def setinfo(self,username,password,domain,origURL): 
  20. '''设置用户登录信息''' 
  21. self.name = username 
  22. self.pwd = password 
  23. self.domain = domain 
  24. self.origURL = origURL 
  25. def login(self): 
  26. '''登录人人网''' 
  27. params = { 
  28. 'domain':self.domain, 
  29. 'origURL':self.origURL, 
  30. 'email':self.name,  
  31. 'password':self.pwd} 
  32. print 'login.......' 
  33. req = urllib2.Request(  
  34. 'http://www.renren.com/PLogin.do', 
  35. urllib.urlencode(params) 
  36. self.file=urllib2.urlopen(req).read()  
  37. newsfeed = open('news.html','w') 
  38. try: 
  39. newsfeed.write(self.file) 
  40. except Exception, e: 
  41. newsfeed.close() 
  42. self.operate = self.opener.open(req)  
  43. print type(self.operate) 
  44. print self.operate.geturl() 
  45. if self.operate.geturl():  
  46. print 'Logged on successfully!' 
  47. self.cj.save('./renren.coockie') 
  48. self.__viewnewinfo() 
  49. else: 
  50. print 'Logged on error' 
  51. def __viewnewinfo(self): 
  52. '''查看好友的更新状态''' 
  53. self.__caiinfo() 
  54. def __caiinfo(self): 
  55. '''采集信息''' 
  56. h3patten = re.compile('<article>(.*?)</article>')#匹配范围 
  57. apatten = re.compile('<h3.+>(.+)</h3>:')#匹配作者 
  58. cpatten = re.compile('</a>(.+)\s')#匹配内容  
  59. content = h3patten.findall(self.file) 
  60. print len(content)  
  61. infocontent = self.operate.readlines() 
  62. print type(infocontent) 
  63. print 'friend newinfo:' 
  64. for i in infocontent: 
  65. content = h3patten.findall(i) 
  66. if len(content) != 0: 
  67. for m in content: 
  68. username = apatten.findall(m) 
  69. info = cpatten.findall(m) 
  70. if len(username) !=0: 
  71. print username[0],'说:',info[0] 
  72. print '----------------------------------------------' 
  73. else: 
  74. continue 
  75. ren = Renren() 
  76. username = 'username'#你的人人网的帐号 
  77. password = 'password'#你的人人网的密码 
  78. domain = 'www.renren.com'#人人网的地址 
  79. origURL = 'http://www.renren.com/home'#人人网登录以后的地址 
  80. ren.setinfo(username,password,domain,origURL) 
  81. ren.login() 

希望本文所述对大家的Python序设计有所帮助。

转载请说明出处
知优网 » python实现自动登录人人网并采集信息的方法

发表评论

您需要后才能发表评论