Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法
本文實例講述了Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法。分享給大家供大家參考。具體如下:
這里演示了Python登錄人人網(wǎng)并抓取新鮮事的方法(抓取后的排版不太美觀~~)
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程序設(shè)計有所幫助。
- 在Python中使用mechanize模塊模擬瀏覽器功能
- 用Python中的wxPython實現(xiàn)最基本的瀏覽器功能
- 使用wxpython實現(xiàn)的一個簡單圖片瀏覽器實例
- 使用python調(diào)用瀏覽器并打開一個網(wǎng)址的例子
- Windows 配置Apache以便在瀏覽器中運行Python script的CGI模式
- Python3.2模擬實現(xiàn)webqq登錄
- python登錄QQ郵箱發(fā)信的實現(xiàn)代碼
- python實現(xiàn)自動登錄人人網(wǎng)并采集信息的方法
- python實現(xiàn)自動登錄人人網(wǎng)并訪問最近來訪者實例
- python人人網(wǎng)登錄應(yīng)用實例
- python實現(xiàn)人人網(wǎng)登錄示例分享
- python cookielib 登錄人人網(wǎng)的實現(xiàn)代碼
- Python腳本簡單實現(xiàn)打開默認瀏覽器登錄人人和打開QQ的方法
相關(guān)文章
pandas.DataFrame 根據(jù)條件新建列并賦值的方法
下面小編就為大家分享一篇pandas.DataFrame 根據(jù)條件新建列并賦值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04使用 Supervisor 監(jiān)控 Python3 進程方式
今天小編就為大家分享一篇使用 Supervisor 監(jiān)控 Python3 進程方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Android+OpenCv4實現(xiàn)邊緣檢測及輪廓繪制出圖像最大邊緣
最近工作需求,需要用opencv來先做一個demo,掃描照片進行邊緣檢測和最大邊緣,就整理出來介紹一下,感興趣的小伙伴們可以參考一下2021-05-05利用Python和OpenCV庫將URL轉(zhuǎn)換為OpenCV格式的方法
這篇文章主要介紹了利用Python和OpenCV庫將URL轉(zhuǎn)換為OpenCV格式的方法,同時用到了NumPy和urllib,需要的朋友可以參考下2015-03-03關(guān)于tensorflow softmax函數(shù)用法解析
這篇文章主要介紹了關(guān)于tensorflow softmax函數(shù)用法解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python smtplib模塊自動收發(fā)郵件功能(一)
這篇文章主要為大家詳細介紹了python smtplib模塊自動收發(fā)郵件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python圖像銳化與邊緣檢測之Sobel與Laplacian算子詳解
圖像銳化和邊緣檢測主要包括一階微分銳化和二階微分銳化,本文主要講解常見的圖像銳化和邊緣檢測方法,即Sobel算子和Laplacian算子,感興趣的可以了解一下2022-12-12