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程序设计有所帮助。

相关文章

scrapy爬虫实例分享

scrapy爬虫实例分享

前一篇文章介绍了很多关于scrapy的进阶知识,不过说归说,只有在实际应用中才能真正用到这些知识。所以这篇文章就来尝试利用scrapy爬取各种网站的数据。 爬取百思不得姐 首先一步一步来...

Python爬取网页中的图片(搜狗图片)详解

Python爬取网页中的图片(搜狗图片)详解

前言 最近几天,研究了一下一直很好奇的爬虫算法。这里写一下最近几天的点点心得。下面进入正文: 你可能需要的工作环境:   Python 3.6官网下载    ...

基于Python实现ComicReaper漫画自动爬取脚本过程解析

基于Python实现ComicReaper漫画自动爬取脚本过程解析

这篇文章主要介绍了基于Python实现ComicReaper漫画自动爬取脚本过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 讲...

利用python爬取斗鱼app中照片方法实例

利用python爬取斗鱼app中照片方法实例

前言 没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。 最近看到斗鱼里的照片都不错,决定用最新学习...

python爬虫 爬取超清壁纸代码实例

python爬虫 爬取超清壁纸代码实例

简介 壁纸的选择其实很大程度上能看出电脑主人的内心世界,有的人喜欢风景,有的人喜欢星空,有的人喜欢美女,有的人喜欢动物。然而,终究有一天你已经产生审美疲劳了,但你下定决定要换壁纸的时候...