python 模拟登录新浪微博
2015-01-30 00:36
423 查看
<p>首先python2.7要安装easy_install</p><p>然后在 easy_install里面安装 lxml</p><p>最后下面是代码,</p>
# -*- coding: utf-8 -*import urllib2import urllibimport cookielibimport lxml.html as HTMLclass Fetcher(object): def __init__(self, username=None, pwd=None, cookie_filename=None): #获取一个保存cookie的对象 self.cj = cookielib.LWPCookieJar() if cookie_filename is not None: self.cj.load(cookie_filename) #将一个保存cookie对象,和一个HTTP的cookie的处理器绑定 self.cookie_processor = urllib2.HTTPCookieProcessor(self.cj) #创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的URL的打开 self.opener = urllib2.build_opener(self.cookie_processor, urllib2.HTTPHandler) #将包含了cookie、http处理器、http的handler的资源和urllib2对象绑定在一起 urllib2.install_opener(self.opener) self.username = username self.pwd = pwd self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer':'','Content-Type':'application/x-www-form-urlencoded'} def get_rand(self, url): headers = {'User-Agent':'Mozilla/5.0 (Windows;U;Windows NT 5.1;zh-CN;rv:1.9.2.9)Gecko/20100824 Firefox/3.6.9', 'Referer':''} req = urllib2.Request(url ,"", headers) login_page = urllib2.urlopen(req).read() rand = HTML.fromstring(login_page).xpath("//form/@action")[0] passwd = HTML.fromstring(login_page).xpath("//input[@type='password']/@name")[0] vk = HTML.fromstring(login_page).xpath("//input[@name='vk']/@value")[0] return rand, passwd, vk def login(self, username=None, pwd=None, cookie_filename=None): if self.username is None or self.pwd is None: self.username = username self.pwd = pwd assert self.username is not None and self.pwd is not None url = 'http://3g.sina.com.cn/prog/wapsite/sso/login.php?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=' # 获取随机数rand、password的name和vk rand, passwd, vk = self.get_rand(url) data = urllib.urlencode({'mobile': self.username, passwd: self.pwd, 'remember': 'on', 'backURL': 'http://weibo.cn/', 'backTitle': '新浪微博', 'vk': vk, 'submit': '登录', 'encoding': 'utf-8'}) url = 'http://3g.sina.com.cn/prog/wapsite/sso/' + rand # 模拟提交登陆 page =self.fetch(url,data) link = HTML.fromstring(page).xpath("//a/@href")[0] if not link.startswith('http://'): link = 'http://weibo.cn/%s' % link # 手动跳转到微薄页面 self.fetch(link,"") # 保存cookie if cookie_filename is not None: self.cj.save(filename=cookie_filename) elif self.cj.filename is not None: self.cj.save() print 'login success!',data def fetch(self, url,data): print 'fetch url: ', url req = urllib2.Request(url,data, headers=self.headers) return urllib2.urlopen(req).read()# 开始运行fet=Fetcher()fet.login("xxxx","xxxxx")下面是登录成功的结果:fetch url: http://3g.sina.com.cn/prog/wapsite/sso/login_submit.php?rand=1516239938&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1 fetch url: http://weibo.cn/register.php?backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=4&revalid=2&ns=1&PHPSESSID=ec7c2aba4da371a7cf4722ff7a4d89e9 login success! backTitle=%E6%96%B0%E6%B5%AA%E5%BE%AE%E5%8D%9A&password_4287=&backURL=http%3A%2F%2Fweibo.cn%2F&remember=on&encoding=utf-8&mobile=jakill%40sina.com&vk=4287_7522_1851253375&submit=%E7%99%BB%E5%BD%95
相关文章推荐
- python模拟登录新浪微博
- 模拟登录新浪微博(Python) - 转
- 模拟登录新浪微博(Python)
- 模拟登录新浪微博(Python)
- python模拟登录新浪微博自动获得调用新浪api所需的code
- Python 模拟登录新浪微博
- python模拟登录新浪微博
- 测试开发Python培训:模拟登录新浪微博-技术篇
- python模拟登录新浪微博
- Python模拟新浪微博登录
- python模拟登录新浪微博
- 测试开发Python培训:模拟登录新浪微博-技术篇
- 测试开发Python培训:模拟登录新浪微博-技术篇 2
- 爬取新浪微博数据+新浪微博模拟登录+mysql+python
- 定向爬虫 - Python模拟新浪微博登录
- 测试开发Python培训:模拟登录新浪微博-技术篇
- [置顶]定向爬虫 - Python模拟新浪微博登录
- 模拟登录新浪微博(Python)
- 【python网络编程】使用rsa加密算法模块模拟登录新浪微博
- Python模拟新浪微博登录