最新消息:20210917 已从crifan.com换到crifan.org

【已解决】允许hi-baidu-mover_v2.py出错:UnboundLocalError: local variable ‘linkNode’ referenced before assignment

Python crifan 1768浏览 0评论

 【已解决】允许hi-baidu-mover_v2.py出错:UnboundLocalError: local variable ‘linkNode’ referenced before assignment

【背景】

想要实现将百度空间搬家到wordpress,网上找了个python脚本工具:

新版本的百度空间到wordpress搬家工具

http://www.yhustc.com/%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84%E7%99%BE%E5%BA%A6%E7%A9%BA%E9%97%B4%E5%88%B0wordpress%E6%90%AC%E5%AE%B6%E5%B7%A5%E5%85%B7.html

中的新版本的工具:

hi-baidu-mover_v2.zip

里面有个

hi-baidu-mover_v2.py

是在以前的

http://blog.cnfol.com/c9u9f3p1b9/article/36267664.html

的BlogMover中的

hi-baidu-mover.py

基础上更新的。

使用此工具之前,先是去下载:

Python 2.5.1 下载

安装好后,再去运行此工具,但是结果始终出错,而且是个语法错误:

 

 E:WebServerWordPressto_wphi-baidu-mover_v2>hi-baidu-mover_v2.py -s http://hi.baidu.com/recommend_music/blog
LINE 143  : INFO     鍑嗗杩炴帴BLOG http://hi.baidu.com/recommend_music/blog
LINE 145  : INFO     杩炴帴鎴愬姛,鏌ユ壘绗竴绡囨棩蹇楃殑鍏ュ彛
LINE 384  : ERROR    鏈煡閿欒
Traceback (most recent call last):
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 382, in <module>
    main()
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 311, in main
    permalink = find1stPermalink(srcURL)
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 151, in find1stPermalink
    if linkNode :
UnboundLocalError: local variable ‘linkNode’ referenced before assignment
Traceback (most recent call last):
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 382, in <module>
    main()
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 311, in main
    permalink = find1stPermalink(srcURL)
  File “E:WebServerWordPressto_wphi-baidu-mover_v2hi-baidu-mover_v2.py”, line 151, in find1stPermalink
    if linkNode :
UnboundLocalError: local variable ‘linkNode’ referenced before assignment

   

【解决过程】

1.网上搜了,结果其他人出现的问题,有些是因为函数内部使用全局变量,而忘了加了global,而导致这类问题,比如这里:

http://www.cnblogs.com/ms_config/articles/1685018.html

http://blog.sina.com.cn/s/blog_4b9eab320100q2l5.html

http://appdemo.iteye.com/category/69366?show_full=true

http://blog.renren.com/share/348105450/7701311104
但是我这里的不是这个原因,所以没参考价值。

 

2.其实很容易看懂错误原因,就是变量使用被引用之前未赋值,很简单的就能想到,加行代码,给其赋值个“空”的值:

linkNode = NULL

结果python中不识别NULL。

而去初始化为空数组:

linkNode = []

倒是可以解决这个语法错误的了。

但是对应的hi-baidu-mover.py执行的结果,完全不是我所想要的,没有实现把百度空间的内容导出为xml,而只生成了头文件。

对应的log为:

 LINE 144   INFO     准备连接BLOG http://hi.baidu.com/recommend_music/blog
LINE 146   INFO     连接成功,查找第一篇日志的入口
LINE 157   ERROR    无法提取日志入口
LINE 342   INFO     日志URL: http://hi.baidu.com/recommend_music/blog
LINE 347   DEBUG    日志标题: ?????ì??
LINE 351   INFO     导出XML文件: hibaidu_12082011-2150.xml
LINE 356   DEBUG    导出文件头部
LINE 362   DEBUG    导出文件尾部
LINE 365   INFO     删除缓存文件
LINE 366   INFO     http://hi.baidu.com/recommend_music/blog 日志搬家成功

   而生成的xml内容为:

 <?xml version=”1.0″ encoding=”UTF-8″?>
<!–
    This is a WordPress eXtended RSS file generated by Live Space Mover as an export of
    your blog. It contains information about your blog’s posts, comments, and
    categories. You may use this file to transfer that content from one site to
    another. This file is not intended to serve as a complete backup of your
    blog.
   
    To import this information into a WordPress blog follow these steps:
   
    1.    Log into that blog as an administrator.
    2.    Go to Manage > Import in the blog’s admin.
    3.    Choose “WordPress” from the list of importers.
    4.    Upload this file using the form provided on that page.
    5.    You will first be asked to map the authors in this export file to users
        on the blog. For each author, you may choose to map an existing user on
        the blog or to create a new user.
    6.    WordPress will then import each of the posts, comments, and categories
        contained in this file onto your blog.
–>

<!– generator=”Live Space Mover 1.0″ created=”2011-12-08 21:50″–>
<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:wp=”http://wordpress.org/export/1.0/
>

<channel>
    <title>?????ì??</title>
    <link>http://hi.baidu.com/recommend_music/blog</link>
    <description></description>
    <pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
    <generator>163 Blog Mover 1.0</generator>
    <language>en</language>
    <wp:wxr_version>1.0</wp:wxr_version>

</channel>
</rss>

此处,之所以很奇怪的是,是由于之前又一次,的确是成功运行了此脚本的,可以实现把百度空间内容抓出来存为xml的。

而此处,无论如何折腾,要么是语法错误,要么执行结果不对。

 

后来还是自己写脚本,搞定了需要的事情。

 

【总结】

此错误,是由于未赋初始值就开始使用变量,解决办法是,先去赋值为空,再使用此值,即可。

关于如何初始化不同类型的变量为空值,请参考:
【整理】python学习心得和体会 v2011-12-21

http://againinput4.blog.163.com/blog/static/1727994912011111103046407/

转载请注明:在路上 » 【已解决】允许hi-baidu-mover_v2.py出错:UnboundLocalError: local variable ‘linkNode’ referenced before assignment

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
99 queries in 0.178 seconds, using 23.41MB memory