Python实现登录人人网并抓取新鲜事的方法

yipeiwu_com5年前Python爬虫

本文实例讲述了Python实现登录人人网并抓取新鲜事的方法。分享给大家供大家参考。具体如下:

这里演示了Python登录人人网并抓取新鲜事的方法(抓取后的排版不太美观~~)

from sgmllib import SGMLParser
import sys,urllib2,urllib,cookielib
class spider(SGMLParser):
  def __init__(self,email,password):
    SGMLParser.__init__(self)
    self.h3=False
    self.h3_is_ready=False
    self.div=False
    self.h3_and_div=False
    self.a=False
    self.depth=0
    self.names=""
    self.dic={}  
    self.email=email
    self.password=password
    self.domain='renren.com'
    try:
      cookie=cookielib.CookieJar()
      cookieProc=urllib2.HTTPCookieProcessor(cookie)
    except:
      raise
    else:
      opener=urllib2.build_opener(cookieProc)
      urllib2.install_opener(opener)    
  def login(self):
    url='http://www.renren.com/PLogin.do'
    postdata={
         'email':self.email,
         'password':self.password,
         'domain':self.domain 
         }
    req=urllib2.Request(
              url,
              urllib.urlencode(postdata)      
              )
    self.file=urllib2.urlopen(req).read()
    #print self.file
  def start_h3(self,attrs):
    self.h3 = True
  def end_h3(self):
    self.h3=False
    self.h3_is_ready=True
  def start_a(self,attrs):
    if self.h3 or self.div:
      self.a=True
  def end_a(self):
    self.a=False
  def start_div(self,attrs):
    if self.h3_is_ready == False:
      return
    if self.div==True:
      self.depth += 1
    for k,v in attrs:
      if k == 'class' and v == 'content':
        self.div=True;
        self.h3_and_div=True  #h3 and div is connected
  def end_div(self):
    if self.depth == 0:
      self.div=False
      self.h3_and_div=False
      self.h3_is_ready=False
      self.names=""
    if self.div == True:
      self.depth-=1
  def handle_data(self,text):
    #record the name
    if self.h3 and self.a:
      self.names+=text
    #record says
    if self.h3 and (self.a==False):
      if not text:pass
      else: self.dic.setdefault(self.names,[]).append(text)
      return
    if self.h3_and_div:
      self.dic.setdefault(self.names,[]).append(text)
  def show(self):
    type = sys.getfilesystemencoding()
    for key in self.dic:
      print ( (''.join(key)).replace(' ','')).decode('utf-8').encode(type), \
         ( (''.join(self.dic[key])).replace(' ','')).decode('utf-8').encode(type)
renrenspider=spider('your email','your password')
renrenspider.login()
renrenspider.feed(renrenspider.file)
renrenspider.show()

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

相关文章

Python爬虫使用脚本登录Github并查看信息

Python爬虫使用脚本登录Github并查看信息

前言分析目标网站的登录方式 目标地址: https://github.com/login    登录方式做出分析: 第一,用form表单方式提交信息, 第二...

利用Python爬取微博数据生成词云图片实例代码

利用Python爬取微博数据生成词云图片实例代码

前言 在很早之前写过一篇怎么利用微博数据制作词云图片出来,之前的写得不完整,而且只能使用自己的数据,现在重新整理了一下,任何的微博数据都可以制作出来,一年一度的虐汪节,是继续蹲在角落默默...

python爬取cnvd漏洞库信息的实例

python爬取cnvd漏洞库信息的实例

今天一同事需要整理http://ics.cnvd.org.cn/工控漏洞库里面的信息,一看960多个要整理到什么时候才结束。 所以我决定写个爬虫帮他抓取数据。 看了一下各类信息还是很规则...

python爬虫入门教程--优雅的HTTP库requests(二)

前言 urllib、urllib2、urllib3、httplib、httplib2 都是和 HTTP 相关的 Python 模块,看名字就觉得很反人类,更糟糕的是这些模块在 Pytho...

python爬取百度贴吧前1000页内容(requests库面向对象思想实现)

此程序以李毅吧为例子,以面向对象的设计思想实现爬取保存网页数据,暂时并未用到并发处理,以后有机会的话会加以改善 首先去百度贴吧分析贴吧地址栏中url后的参数,找到分页对应的参数p...