码迷,mamicode.com
首页 > 其他好文 > 详细

123

时间:2015-03-05 23:51:01      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:

我的个人博客( 肥龙的博客)发表了新文章了!
欢迎大家过来阅读,以下是文章的连接地址

http://www.comingcode.com/?p=367


# -*- coding: utf8 -*- ‘‘‘ Created on 2015-01-22 @author : dragon ‘‘‘ import urllib2 import urllib import cookielib import hashlib import re class OSC: def __init__(self): self.usercode = "" self.userid = "" self.cj = cookielib.LWPCookieJar(); self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)); urllib2.install_opener(self.opener); #登录 def login(self, name, pwd): loginPostData = { ‘email‘ : name, ‘pwd‘ : hashlib.sha1(pwd).hexdigest() #密码SHA1加密 }; #必须设置head否则返回403拒绝 loginRequest = urllib2.Request(‘https://www.oschina.net/action/user/hash_login‘,urllib.urlencode(loginPostData)); loginRequest.add_header(‘Accept‘,‘*/*‘); loginRequest.add_header(‘Accept-Language‘,‘zh-CN,zh;q=0.8‘); loginRequest.add_header(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36‘); loginRequest.add_header(‘Content-Type‘,‘application/x-www-form-urlencoded‘); sendPost = urllib2.urlopen(loginRequest); #获取页面中的user_code和 user #user_code应该是账号加密后的结果(用base64解码为乱码,但是可以看出是"XXXX:账号"的格式),user应该是用户唯一id,不明白这里为什么不直接用username reqRequest = urllib2.Request(‘http://www.oschina.net/‘); reqRequest.add_header(‘Accept‘,‘*/*‘); reqRequest.add_header(‘Accept-Language‘,‘zh-CN,zh;q=0.8‘); reqRequest.add_header(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36‘); reqRequest.add_header(‘Content-Type‘,‘application/x-www-form-urlencoded‘); content = urllib2.urlopen(reqRequest).read(); #用bs4不知什么原因不能用name获取值,折腾半天直接用正则吧 matchVal = re.search(u‘name=‘user_code‘ value=‘(?P.*?)‘/>‘,content); self.usercode = matchVal.group(‘user_code‘); matchVal = re.search(u‘name=‘user‘ value=‘(?P.*?)‘/>‘,content); self.userid = matchVal.group(‘user_id‘); #发动弹 def SendPopMsg(self, msg): #终于可以发动弹了 dtPostData = { ‘user_code‘ : self.usercode, ‘user‘ : self.userid, ‘msg‘: msg.encode("utf8") }; dtRequest = urllib2.Request(‘http://www.oschina.net/action/tweet/pub‘,urllib.urlencode(dtPostData)); dtRequest.add_header(‘Accept‘,‘*/*‘); dtRequest.add_header(‘Accept-Language‘,‘zh-CN,zh;q=0.8‘); dtRequest.add_header(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36‘); dtRequest.add_header(‘Content-Type‘,‘application/x-www-form-urlencoded‘); sendPost = urllib2.urlopen(dtRequest); #发送博文 def SendBlog(self, title, content, isPrivate): postdata = { ‘user_code‘: self.usercode, ‘draft‘: 176444, ‘title‘: title.encode("utf8"), ‘catalog‘: 407791, ‘abstracts‘: ‘‘, ‘content‘: content.encode("utf8"), ‘content_type‘: 2, ‘classification‘: 430381, ‘type‘: 1, ‘privacy‘: isPrivate, ‘deny_comment‘: 0, ‘auto_content‘: 1, ‘as_top‘: 0 } loginRequest = urllib2.Request(‘https://www.oschina.net/action/blog/save?‘,urllib.urlencode(postdata)); loginRequest.add_header(‘Accept‘,‘*/*‘); loginRequest.add_header(‘Accept-Language‘,‘zh-CN,zh;q=0.8‘); loginRequest.add_header(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36‘); loginRequest.add_header(‘Content-Type‘,‘application/x-www-form-urlencoded‘); sendPost = urllib2.urlopen(loginRequest); if __name__ == "__main__": test = OSC() test.login(‘feilong.luo@gmail.com‘, ‘luofeilong‘) test.SendPopMsg("动弹一下")

123

标签:

原文地址:http://my.oschina.net/dragonblog/blog/383171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!