#!/usr/bin/env python
#coding:utf8
import urllib,urllib2,httplib,cookielib,os,re,sys,time,md5
import pdb
def login(userdata,posturl):
cookie=cookielib.CookieJar()
cj=urllib2.HTTPCookieProcessor(cookie)
request=urllib2.Request(posturl)
opener=urllib2.build_opener(cj)
c = opener.open(request,urllib.urlencode(userdata))
bincontent= c.read()
print bincontent
return opener
def postdata (opener,data,posturl):
loginrequest=urllib2.Request(posturl)
print data
c = opener.open(loginrequest,urllib.urlencode(data))
bincontent = c.read()
#print bincontent
def postbaidu (data):
userdata = {'password':'iloveyou365','username':'riverbird2005','tpl':'sp','tpl_reg':'sp','u':'','Submit':' 登录 '}
opener = login(userdata,'')
#baidudata = {'cm':1,'ct':1,'spBlogCatName':unicode('分类',"utf-8").encode("gb2312"),'spBlogPower':0,'spBlogText':unicode(data['content'],"utf-8").encode("gb2312"),'spBlogTitle':unicode(data['title'],"utf-8").encode("gb2312"),'spIsCmtAllow':1,'spRefURL':'riverbird/creat/blog/','spVcode':'','spVerifyKey':'','tj':''}
baidudata = {'cm':1,
'ct':1,
'spBlogCatName':unicode('分类',"utf-8").encode("gb2312"),
'spBlogPower':0,
'spBlogText':unicode(data['content'],"utf-8").encode("gb2312"),
'spBlogTitle':unicode(data['title'],"utf-8").encode("gb2312"),
'spIsCmtAllow':1,
'spRefURL':'riverbird/creat/blog/',
'spVcode':'',
'spVerifyKey':'',
'tj':''}
postdata(opener,baidudata,'riverbird/commit')
"""
def postsina (data):
userdata = {'loginname':'用户','passwd':'密码'}
opener = login(userdata,'http://my.blog.sina.com.cn/login.php?index=index&type=new')
sinadata = {'album':'','blog_body':data['content'],'blog_class':2,'blog_id':'','blog_title':data['title'],'is2bbs':1,'is_album':0,'is_media':'','join_circle':1,'sina_sort_id':'134','stag':'','tag':data['tag'],'time':'','x_cms_flag':1}
postdata(opener,sinadata,'http://control.blog.sina.com.cn/admin/article/article_post.php')
def postsohu (data):
m = md5.md5('密码')
userdata = {'appid':'1019','b':'1','password':m.hexdigest(),'persistentcookie':0,'pwdtype':'1','s':'1213527861109','userid':'用户名@sohu.com','w':'1280'}
opener = login(userdata,'')
postpage=urllib2.Request('http://blog.sohu.com/manage/entry.do?m=add&t=shortcut')#
c = opener.open(postpage)
bincontent= c.read()
p=re.compile(r'''\s+" name="aid" value="(.*)">\s+''',re.M)#
c = p.findall(bincontent)
print c
if len(c)>0:
sohudata = {'aid':c[0],'allowComment':2,'categoryId':0,'contrCataId':'','contrChId':'','entrycontent':unicode(data['content'],"utf-8").encode("gbk"),'entrytitle':unicode(data['title'],"utf-8").encode("gbk"),'excerpt':'','keywords':unicode(data['tag'],"utf-8").encode("gbk"),'m':'save','newGategory':'','oper':'art_ok','perm':'0','save':'-','shortcutFlag':'true'}
postdata(opener,sohudata,'http://blog.sohu.com/manage/entry.do')
"""
def main ():
pdata = {'title':'title','content':'content','tag':"tag1 tag2"}
#postsohu(pdata)
#postsina(pdata)
postbaidu(pdata)
if __name__ == '__main__':
main()
|