欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python cookielib 登錄人人網(wǎng)的實(shí)現(xiàn)代碼

 更新時(shí)間:2012年12月19日 23:27:20   作者:  
今天晚上不是很忙,所以早早的就在電腦的旁邊開始寫東西了。我今天給大家分享一個(gè)我自己用python寫的自動(dòng)登錄 人人網(wǎng)的腳本,沒辦法就是懶!懶的輸入帳號(hào)和密碼,讓python給我們減少工作量

先上腳本吧,等下來講下知識(shí)點(diǎn):

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
#encoding=utf-8
import sys
import re
import urllib2
import urllib
import cookielib

class Renren(object):

def __init__(self):
self.name = self.pwd = self.content = self.domain = self.origURL = ''
self.operate = ''#登錄進(jìn)去的操作對(duì)象
self.cj = cookielib.LWPCookieJar()
try:
self.cj.revert('renren.coockie')
except Exception,e:
print e

self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)


def setinfo(self,username,password,domain,origURL):
'''設(shè)置用戶登錄信息'''
self.name = username
self.pwd = password
self.domain = domain
self.origURL = origURL

def login(self):
'''登錄人人網(wǎng)'''
params = {'domain':self.domain,'origURL':self.origURL,'email':self.name, 'password':self.pwd}
print 'login.......'
req = urllib2.Request(
'http://www.renren.com/PLogin.do',
urllib.urlencode(params)
)

self.operate = self.opener.open(req)

if self.operate.geturl() == 'http://www.renren.com/Home.do':
print 'Logged on successfully!'
self.cj.save('renren.coockie')
self.__viewnewinfo()
else:
print 'Logged on error'

def __viewnewinfo(self):
'''查看好友的更新狀態(tài)'''
self.__caiinfo()


def __caiinfo(self):
'''采集信息'''

h3patten = re.compile('<h3>(.*?)</h3>')#匹配范圍
apatten = re.compile('<a.+>(.+)</a>:')#匹配作者
cpatten = re.compile('</a>(.+)\s')#匹配內(nèi)容
infocontent = self.operate.readlines()
# print infocontent
print 'friend newinfo:'
for i in infocontent:
content = h3patten.findall(i)
if len(content) != 0:
for m in content:
username = apatten.findall(m)
info = cpatten.findall(m)
if len(username) !=0:
print username[0],'說:',info[0]
print '----------------------------------------------'
else:
continue

ren = Renren()
username = ''#你的人人網(wǎng)的帳號(hào)
password = ''#你的人人網(wǎng)的密碼
domain = 'renren.com'#人人網(wǎng)的地址
origURL = 'http://www.renren.com/Home.do'#人人網(wǎng)登錄以后的地址
ren.setinfo(username,password,domain,origURL)
ren.login()

主要用到了python cookielib,urllib2,urllib這3個(gè)模塊,這3個(gè)模塊是python做http這方面比較好的模塊.


self.cj = cookielib.LWPCookieJar()

try:
self.cj.revert('renren.coockie')
except Exception,e:
print e
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))

urllib2.install_opener(self.opener)
這幾行是在本地建立人人網(wǎng)的cookies,因?yàn)槿巳司W(wǎng)要驗(yàn)證cookies才能登錄,你運(yùn)行這個(gè)腳本的話,會(huì)發(fā)現(xiàn)在當(dāng)前目錄 有個(gè)程序會(huì)自動(dòng)建立一個(gè)renren.cookie這個(gè)文件。

我這里renren.cookie的信息是: #LWP-Cookies-2.0 Set-Cookie3: WebOnLineNotice_244225225=1; path="/"; domain=".renren.com"; path_spec; domain_dot; expires="2010-04-11 06:59:33Z"; version=0 總結(jié)一下如果網(wǎng)站登錄要用cookie的話,就要用到cookielib這個(gè)模塊,不然你用程序登錄不了網(wǎng)站,過斷時(shí)間在寫個(gè)urlib的例子,大家可以先用上面這個(gè)腳本玩玩!體會(huì)下python 人人網(wǎng)代碼的樂趣!

相關(guān)文章

最新評(píng)論