這篇文章將為大家詳細(xì)講解有關(guān)Python怎么爬取人人網(wǎng)新鮮事,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
十年的蜀山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整蜀山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“蜀山網(wǎng)站設(shè)計(jì)”,“蜀山網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。