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

