免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2539 | 回复: 1
打印 上一主题 下一主题

崩溃了,在给百度空间写自动post博客时 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-01 19:10 |只看该作者 |倒序浏览


这段时间看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+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()


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15586/showart_1887534.html

论坛徽章:
0
2 [报告]
发表于 2009-04-05 18:25 |只看该作者
应该尽量用   PIL 去实现 验证码识别
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP