国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現自動登錄人人網并采集信息的方法

2020-01-04 18:06:18
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了python實現自動登錄人人網并采集信息的方法,涉及Python模擬登陸及正則匹配的相關技巧,需要的朋友可以參考下

本文實例講述了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序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福泉市| 通化县| 城市| 改则县| 漠河县| 杭锦后旗| 类乌齐县| 新余市| 兴安盟| 安阳县| 九寨沟县| 绍兴县| 涪陵区| 景洪市| 鄂托克旗| 通州市| 多伦县| 宁乡县| 萍乡市| 迁安市| 屏东县| 梁河县| 武安市| 长泰县| 九龙县| 张家川| 突泉县| 乐清市| 通河县| 巴东县| 邮箱| 盐城市| 城市| 石首市| 益阳市| 连江县| 海伦市| 五莲县| 云浮市| 万宁市| 泸西县|