Python怎么爬取人人網(wǎng)新鮮事

這篇文章將為大家詳細講解有關Python怎么爬取人人網(wǎng)新鮮事,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

十年的蜀山網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整蜀山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“蜀山網(wǎng)站設計”,“蜀山網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法:

from sgmllib import SGMLParser
import sys,urllib2,urllib,cookielib
class spider(SGMLParser):
  def __init__(self,email,password):
    SGMLParser.__init__(self)
    self.h4=False
    self.h4_is_ready=False
    self.div=False
    self.h4_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_h4(self,attrs):
    self.h4 = True
  def end_h4(self):
    self.h4=False
    self.h4_is_ready=True
  def start_a(self,attrs):
    if self.h4 or self.div:
      self.a=True
  def end_a(self):
    self.a=False
  def start_div(self,attrs):
    if self.h4_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.h4_and_div=True  #h4 and div is connected
  def end_div(self):
    if self.depth == 0:
      self.div=False
      self.h4_and_div=False
      self.h4_is_ready=False
      self.names=""
    if self.div == True:
      self.depth-=1
  def handle_data(self,text):
    #record the name
    if self.h4 and self.a:
      self.names+=text
    #record says
    if self.h4 and (self.a==False):
      if not text:pass
      else: self.dic.setdefault(self.names,[]).append(text)
      return
    if self.h4_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怎么爬取人人網(wǎng)新鮮事就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞名稱:Python怎么爬取人人網(wǎng)新鮮事
網(wǎng)站URL:http://muchs.cn/article34/ghgpse.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、做網(wǎng)站、移動網(wǎng)站建設手機網(wǎng)站建設、、響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司