Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法
更新時間:2015年05月11日 09:43:59 作者:斷鴻
這篇文章主要介紹了Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法,可實現(xiàn)Python模擬登陸并抓取新鮮事的功能,需要的朋友可以參考下
本文實例講述了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)文章
Python如何通過內(nèi)存管理提升程序執(zhí)行效率
Python提供了自動內(nèi)存管理的功能,但是如果不小心使用,可能會導(dǎo)致內(nèi)存泄漏和性能問題,所以巧妙使用內(nèi)存管理是提高Python執(zhí)行效率的關(guān)鍵,下面就來和大家仔細講講Python的內(nèi)存管理技巧吧2023-06-06
Python實現(xiàn)視頻轉(zhuǎn)換為音頻的方法詳解
這篇文章主要為大家詳細Python如何將視頻轉(zhuǎn)換為音頻并將音頻文件保存到特定文件夾下,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
Python趣味挑戰(zhàn)之實現(xiàn)簡易版音樂播放器
小伙伴們天天學(xué)編程應(yīng)該都學(xué)累了,今天特地給大家整理了這篇文章,讓大家在學(xué)習(xí)的時候也收貨快樂,文中有非常詳細的代碼示例,需要的朋友可以參考下2021-05-05
Python進制轉(zhuǎn)換與反匯編實現(xiàn)流程介紹
這篇文章主要介紹了Python進制轉(zhuǎn)換與反匯編的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10
Python辦公自動化之?dāng)?shù)據(jù)可視化與報表生成
在現(xiàn)代辦公環(huán)境中,數(shù)據(jù)處理和報表生成是一項重要的任務(wù),本文將高效介紹如何使用Python進行數(shù)據(jù)可視化和報表生成,讓您的辦公工作更加順利2023-07-07

