UChome 日志导出
因为纸飞机(http://my.nuaa.edu.cn)的外网开通一直没有着落,也可能因为一些其他的各种原因,Sherry 想要将纸飞机空间 UChome 里的日志导出,用以导入到其他的博客托管服务上,不过很可惜的是 UChome 本身并不提供日志导出这一功能,而且也没有提供日志的 RSS Feed,仅有的一个订阅分享输出的日志都还是截断过的摘要,这样一来就不好办了,想要达到目标只能尝试使用其他的一些手段。
首先想到的是管理员直接操作数据库,这个最为简单,直接...不过很显然,我不是管理员,所以这个方法暂时放一边。
第二个可行的方法就是编写程序,分别访问作者的每一篇日志,并且解析 html 文件,从中提取出作者,分类,文章内容,发布日期等要素,自己再重组输出为 xml 文件,于是可以使用这个 xml 文件导入到其他的博客系统。
鉴于条件限制,我貌似只能使用第二种方法,访问日志页面?还是用 curl 顺手,至于解析 html 文件,上次使用 Python 的 BeautifulSoup 模块很有感觉的样子...那么就开始了
飞机的页面设置了权限,非登录用户无法查看别人的页面,所以第一步是要登录纸飞机空间,使用上篇文章中提到过的代码
- import os,re,urllib,codecs,time
- from BeautifulSoup import BeautifulSoup
- print u'登录飞机...'
- os.system(curl+ ' \"http://my.nuaa.edu.cn/home/do.php?ac=a4d30c424df67e23cdefc40e489f82c2&&ref\" >enuaa_temp.txt 2>nul')
- enuaa_web=BeautifulSoup(file(r'enuaa_temp.txt').read())
- formhash=enuaa_web.find('input',attrs={'name':'formhash'})['value']
- os.system(curl +' -c enuaa.txt -d \"username='+ user +'&password='+ password +'&cookietime=315360000&refer=space.php?do\=home&loginsubmit=true&formhash='+ formhash +'\" \"http://my.nuaa.edu.cn/home/do.php?ac=a4d30c424df67e23cdefc40e489f82c2&&ref\" >nul')
接下来访问日志列表的页面,其地址组成为:http://my.nuaa.edu.cn/home/space-'+ uid +'-do-blog-view-me-page-'+ page_num +'.html ,其中的 UID 就是账户的一个身份标识,可以直接查看,page_num为页数,所以可以带cookie访问这个页面,输出到文件后用 BeautifulSoup 来解析,提取出每一篇文章的地址
- os.system(curl+ ' -b enuaa.txt \"http://my.nuaa.edu.cn/home/space-'+ uid +'-do-blog-view-me-page-'+ page_num +'.html\" >enuaa_temp.txt 2>nul')
- os.system('\"'+ convertz + '\" /o:utf8 enuaa_temp.txt enuaa_temp_ut.txt')
- enuaa_web=BeautifulSoup(file(r'enuaa_temp_ut.txt').read())
- for i in range(0,10):
- blog_url=enuaa_web.findAll('div',attrs={'class':'title'})[i].h4.a['href']
- ExportToXML(blog_url)
同样地,然后再访问获取到的单篇文章页面,并使用 BeautifulSoup 来提取信息,在这个步骤上需要注意的是,如果 BeautifulSoup 解析发生非正常的莫名其妙的错误的时候,可能需要换用旧的 3.0.8 版本,我不是太清楚 rss 规范,所以部分参照 wordpress 的输出,最终的代码如下:
- #!/usr/bin/python
- #coding=utf-8
- import os,re,urllib,codecs,time
- from BeautifulSoup import BeautifulSoup
- curl=r'g:\curl\curl.exe'
- convertz=r'g:\curl\convertz\convertz.exe'
- user=''
- password=''
- uid='100395'
- page_num=7
- last_num=2
- author=u'sherry'
- def ExportToXML(blog_url):
- os.system(curl+ ' -b enuaa.txt \"http://my.nuaa.edu.cn/home/'+ blog_url +'\" >enuaa_temp.txt 2>nul')
- os.system('\"'+ convertz + '\" /o:utf8 .\\enuaa_temp.txt .\\enuaa_post.txt')
- os.system('copy enuaa_post.txt enuaa_post.html /y >nul')
- blog_post=BeautifulSoup(file(r'enuaa_post.html').read())
- ss=u'正在导出日志: '+ blog_post.findAll('h1')[1].string +' ...'
- print ss
- bb=''' <item>
- <title>'''
- bb=bb + blog_post.findAll('h1')[1].string + '''</title>'''
- print >>yunmengze,bb
- bb=''' <link>http://my.nuaa.edu.cn/home/'''+ blog_url +'''</link>'''
- print >>yunmengze,bb
- bb=''' <pubDate>''' + blog_post.findAll('span',attrs={'class':'gray'})[1].string + '''</pubDate>'''
- print >>yunmengze,bb
- bb=''' <dc:creator>'''+ author +'''</dc:creator>
- <category><![CDATA[ Uncategorized ]]></category>
- <description></description>'''
- print >>yunmengze,bb
- bb=''' <content:encoded><![CDATA[''' + str(blog_post.findAll('div',attrs={'id':'blog_article'})[0]).decode('utf-8') + ''']]></content:encoded>'''
- print >>yunmengze,bb
- # bb=''' <wp:post_date>''' + blog_post.findAll('span',attrs={'class':'gray'})[1].string + '''</wp:post_date>'''
- # print >>yunmengze,bb
- bb=''' </item>'''
- print >>yunmengze,bb
- def ExportBlogUrl(uid,page_num):
- os.system(curl+ ' -b enuaa.txt \"http://my.nuaa.edu.cn/home/space-'+ uid +'-do-blog-view-me-page-'+ page_num +'.html\" >enuaa_temp.txt 2>nul')
- os.system('\"'+ convertz + '\" /o:utf8 .\\enuaa_temp.txt .\\enuaa_temp_ut.txt')
- enuaa_web=BeautifulSoup(file(r'enuaa_temp_ut.txt').read())
- for i in range (0,10):
- if i==4 :
- continue
- else :
- blog_url=enuaa_web.findAll('div',attrs={'class':'title'})[i].h4.a['href']
- ExportToXML(blog_url)
- else :
- for i in range(0,10):
- blog_url=enuaa_web.findAll('div',attrs={'class':'title'})[i].h4.a['href']
- ExportToXML(blog_url)
- print u'登录飞机...'
- os.system(curl+ ' \"http://my.nuaa.edu.cn/home/do.php?ac=a4d30c424df67e23cdefc40e489f82c2&&ref\" >enuaa_temp.txt 2>nul')
- enuaa_web=BeautifulSoup(file(r'enuaa_temp.txt').read())
- formhash=enuaa_web.find('input',attrs={'name':'formhash'})['value']
- os.system(curl +' -c enuaa.txt -d \"username='+ user +'&password='+ password +'&cookietime=315360000&refer=space.php?do\=home&loginsubmit=true&formhash='+ formhash +'\" \"http://my.nuaa.edu.cn/home/do.php?ac=a4d30c424df67e23cdefc40e489f82c2&&ref\" >nul')
- os.system('del /q enuaa_temp.txt')
- yunmengze=codecs.open(author +'.xml','w','utf-8')
- bb=u'''<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" - xmlns:content="http://purl.org/rss/1.0/modules/content/"
- xmlns:wfw="http://wellformedweb.org/CommentAPI/"
- xmlns:dc="http://purl.org/dc/elements/1.1/"
- xmlns:atom="http://www.w3.org/2005/Atom"
- xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
- xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
- >
<!-- 该文件由起衣导出 http://isouth.org --> - <channel>
- <title>'''+ author +u''' 的日志</title>
<link>http://yunmengze.net</link> - <description>斜阳院落,柳边深巷</description>
- <pubDate>'''+ time.asctime() +'''</pubDate>
- <language>zh-cn</language>
- <sy:updatePeriod>hourly</sy:updatePeriod>
- <sy:updateFrequency>1</sy:updateFrequency>
- <generator>http://yunmengze.net</generator>
- '''
- print >>yunmengze,bb
- for i in range(1,page_num+1):
- ExportBlogUrl(uid,str(i))
- bb=''' </channel>
- </rss>'''
- print >>yunmengze,bb
- yunmengze.close()
- os.system('del /q enuaa_temp_ut.txt >nul')
- os.system('del /q enuaa_post.html >nul')
- os.system('del /q enuaa_temp.txt >nul')
- os.system('del /q enuaa_post.txt >nul')
- os.system('del /q enuaa.txt >nul')
然后我将生成的 sherry.xml 导入 wordpress ,成功!
本文链接: http://isouth.org/archives/278.html , 转载请注明出处,此外还可以订阅我。
相关日志 Relate Posts
“UChome 日志导出”19条留言
Pingbacks/Trackbacks
-
[...] This post was mentioned on Twitter by 起衣, 起衣. 起衣 said: UChome 日志导出 http://isouth.org/archives/278.html [...]
沙发哦~~~
@权子 第一次呀
@起衣
嗯,感觉这个的日志功能还是太复杂了~-~ 谁谁想要个~
@littlewater 可能需要针对性的才有效
@起衣
=3= 你就爱折腾- -
@littlewater 这次是帮忙嘛
@起衣
多好的功能,只可惜了没有办法帮到水水\ \~
@sherrychiaki 发现…脚本还有很多地方需要修改,不过写它的目的只是给你导出日志,现在目的达到了,就不再理会叻
Uchome是什么我不知道呢
@软件盒子 那个和Discuz!一起的康盛的空间产品…
嘿嘿 起衣真牛逼啊,来学习学习
@ishagua
是看出了我的无聊…折腾
GFW blog 转载了这篇 这里www.chinagfw.org/2010/11/15.html
@why 偶也,多谢风笛?GFW转过几篇叻~
不好 暴露了
@why 你早暴露叻额…
这个方法使用与其他的吗