您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Python怎么爬取人人網(wǎng)新鮮事,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
Python實(shí)現(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()
關(guān)于Python怎么爬取人人網(wǎng)新鮮事就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。