python实现自动登录人人网并采集信息的方法

作者:不吃皮蛋 时间:2021-07-27 05:44:24 

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


#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import re
import urllib2
import urllib
import cookielib
class Renren(object):
 def __init__(self):
   self.name = self.pwd = self.content = self.domain = self.origURL = ''
   self.operate = ''#登录进去的操作对象
   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)
 def setinfo(self,username,password,domain,origURL):
   '''设置用户登录信息'''
   self.name = username
   self.pwd = password
   self.domain = domain
   self.origURL = origURL
 def login(self):
   '''登录人人网'''
   params = {
     'domain':self.domain,
     'origURL':self.origURL,
     'email':self.name,
     'password':self.pwd}
   print 'login.......'
   req = urllib2.Request(
     'http://www.renren.com/PLogin.do',
     urllib.urlencode(params)
   )
   self.file=urllib2.urlopen(req).read()    
   newsfeed = open('news.html','w')
   try:
     newsfeed.write(self.file)
   except Exception, e:
     newsfeed.close()
   self.operate = self.opener.open(req)
   print type(self.operate)
   print self.operate.geturl()
   if self.operate.geturl():
     print 'Logged on successfully!'
     self.cj.save('./renren.coockie')
     self.__viewnewinfo()
   else:
     print 'Logged on error'
 def __viewnewinfo(self):
   '''查看好友的更新状态'''
   self.__caiinfo()
 def __caiinfo(self):
   '''采集信息'''    
   h3patten = re.compile('<article>(.*?)</article>')#匹配范围
   apatten = re.compile('<h3.+>(.+)</h3>:')#匹配作者
   cpatten = re.compile('</a>(.+)\s')#匹配内容
   content = h3patten.findall(self.file)
   print len(content)  
   infocontent = self.operate.readlines()
   print type(infocontent)
   print 'friend newinfo:'
   for i in infocontent:
     content = h3patten.findall(i)
     if len(content) != 0:
       for m in content:
         username = apatten.findall(m)
         info = cpatten.findall(m)
         if len(username) !=0:
           print username[0],'说:',info[0]
           print '----------------------------------------------'
         else:
           continue
ren = Renren()
username = 'username'#你的人人网的帐号
password = 'password'#你的人人网的密码
domain = 'www.renren.com'#人人网的地址
origURL = 'http://www.renren.com/home'#人人网登录以后的地址
ren.setinfo(username,password,domain,origURL)
ren.login()

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

标签:python,登录,人人网,采集
0
投稿

猜你喜欢

  • Python批量启动多线程代码实例

    2021-09-01 04:41:16
  • python每次处理固定个数的字符的方法总结

    2021-06-23 15:45:48
  • sql如何删除数据库中数据记录

    2008-10-09 12:31:00
  • 详解python中的文件与目录操作

    2023-10-15 06:15:03
  • 如何应对SQL Server数据库崩溃

    2008-11-24 17:25:00
  • 详解go语言json的使用技巧

    2023-09-12 10:27:48
  • Python装饰器的定义和使用详情

    2021-01-26 23:22:15
  • SQL SERVER 与ACCESS、EXCEL的数据转换方法分享

    2012-02-25 20:17:22
  • Python实现简单图像缩放与旋转

    2022-02-19 05:48:17
  • PHP基础之运算符的使用方法

    2023-11-20 17:33:55
  • python 实现倒计时功能(gui界面)

    2021-03-05 14:19:55
  • 两个非常规ASP木马(可躲过扫描)

    2011-03-11 10:38:00
  • Python使用chardet判断字符编码

    2021-05-14 03:03:52
  • JS版的date函数(和PHP的date函数一样)

    2023-11-15 02:02:11
  • PHP CURL CURLOPT参数说明(curl_setopt)

    2023-11-14 19:06:44
  • Java使用正则表达式(regex)匹配中文实例代码

    2023-06-17 07:59:46
  • Python处理Excel文件实例代码

    2022-02-15 23:13:01
  • Django 多语言教程的实现(i18n)

    2022-12-18 22:41:39
  • 如何利用Python和matplotlib更改纵横坐标刻度颜色

    2022-06-02 04:00:03
  • JavaScript错误处理

    2023-09-23 08:26:37
  • asp之家 网络编程 m.aspxhome.com