文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>崩溃了,在给百度空间写自动post博客时

崩溃了,在给百度空间写自动post博客时

时间:2009-04-01  来源:riverbird2005

这段时间看baidu空间速度挺快,而且服务上面还可以,就想将自己的备用博客迁到百度上面。但是,百度之前的搬家公司现在已经没有了,我是程序员我怕谁,所以,便想自己通过html post的方法来搞定他吧。用python写脚本,先实现自动发文章部分,但是在登录的时候就出问题了。先看代码:

#!/usr/bin/python
import urllib,urllib2,cookielib
import pdb
def test():
        cj = cookielib.CookieJar()
        #pdb.set_trace()
        url_login = "https://passport.baidu.com/?login"
        #body = (('username','xxx'),('password','xxx'),("verifycode","YRAE"))
        body = (('username','xxx'),('password','xxx'))
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        #opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')]
        opener.addheaders = [('User-agent', 'Opera/9.23')]
        urllib2.install_opener(opener)
        req = urllib2.Request(url_login,urllib.urlencode(body))
        u = urllib2.urlopen(req)
        print u.read()

test()


最后抓了一下结果,看到,说是“请输入验证码",我在web上面并没有看到这样的提示(firefox下),看html源码发现的确是需要的。因为验证码是图片,所以最终没有再有耐心搞定去了。网上提供了两种办法,一种是进行图像分析处理,另一种是半自动方法:先将图片打开,然后用户自己再输入,不过这样一来就失去了最初的意义。
下面是找的一段在百度空间发文章的程序,很实用,但是依旧在登录验证码上卡住了。

#!/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':'http://hi.baidu.com/','Submit':' 登录 '}
        opener = login(userdata,'https://passport.baidu.com/?login')
        #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':'http://hi.baidu.com/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':'http://hi.baidu.com/riverbird/creat/blog/',
                     'spVcode':'',
                     'spVerifyKey':'',
                     'tj':''}
        postdata(opener,baidudata,'http://hi.baidu.com/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,'http://passport.sohu.com/sso/login.jsp')
        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+<input type="hidden" 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()

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载