代码如下:
#_*_coding:utf_8_
from sgmllib import SGMLParser
import sys, urllib2, urllib, cookielib
import datetime, time
class spider(SGMLParser):
def __init__(self, email, password):
SGMLParser.__init__(self)
self.email = email
self.password = password
self.domain = 'renren.com'
try:
cookie = cookielib.CookieJar()
# a class to handle HTTP cookies
cookieProc = urllib2.HTTPCookieProcessor(cookie)
except:
raise
else:
opener = urllib2.build_opener(cookieProc)
urllib2.install_opener(opener)
def login(self):
print '开始登录'
url = 'http://www.renren.com/PLogin.do'
#url = 'http://www.renren.com/SysHome.do'
postdata = {
'email': self.email,
'password': self.password,
'domain': self.domain
}
# 一般情况下引入urllib2的地方都需要引入urllib,因为需要urlencode()
req = urllib2.Request(
url,
urllib.urlencode(postdata)
)
self.file = urllib2.urlopen(req).read()
新闻热点
疑难解答