Author: Xuan Hu

  • 在Eclipse中运行pdfbox

    【前言】pdfbox是实验室做文档识别需要了解的开源项目,可是官网doc过于简略,网上资料很多只是调用jar并非在Eclipse中配置运行源代码,仅以此文祭奠我两天的折腾时间……

    0) Environment Specification
    OS:Windows 7
    IDE:Eclipse SDK 3.7.0
    JDK:Version 6 Update 26

    1) This post will not involve the configuration of Java, but you need to confirm JAVA_HOME is in your “Environment Variable” which we will need later on.
    The key should be “JAVA_HOME” and the value is the path of JDK, for me is “D:Program FilesJavajdk1.6.0_26”

    2) Download and extract pdfbox, pdfbox-*.*.*-src.zip, for me is “pdfbox-1.6.0-src.zip” and the extact location is “E:CodeJAVApdfbox-1.6.0”
    http://pdfbox.apache.org/download.html

    3) Download and Extract Maven2, apache-maven-*.*.*-bin.zip, for me is “apache-maven-3.0.3-bin.zip” and the extract location is “D:Program Filesapache-maven-3.0.3”
    http://maven.apache.org/download.html

    4) Configure the “Environment Variable” for Maven2 / Install Maven2
    4.1) Add key “M2_HOME” and the value is path you extact Maven2, for me is “D:Program ilesapache-maven-3.0.3”
    4.2) Add “%M2_HOME%bin” to the key “Path”.
    4.3) Open a new command prompt and run “mvn –version” to verify correct installation.

    5) Install and configure the pdfbox via Maven2
    5.1) Open a new command prompt and change directory to the extract location of pdfbox
    5.2) Run “mvn clean install” to install the pdfbox
    5.3) Run “mvn eclipse:eclipse” to make it a eclipse project.
    5.4) Run “mvn -Declipse.workspace=”PATH OF THE WORKSPACE” eclipse:configure-workspace” to add M2_REPO classpath variable to Eclipse.
    Note : 5.3 & 5.4 is the MOST IMPORTANT part.

    6) Import the project into Eclipse with the root directory of pdfbox (for me is E:CodeJAVApdfbox-1.6.0) then there should be no errors in all the projects.

    7) Edit the “Run Configurations” and enter the command line arguments in the “Arguments” tab and then everything should run correctly.

    Reference:
    1) http://pdfbox.apache.org/userguide/building_pdfbox.html
    2) http://maven.apache.org/download.html#Installation
    3) http://maven.apache.org/guides/getting-started/index.html
    4) http://www.mkyong.com/maven/how-to-configure-m2_repo-variable-in-eclipse-ide/

  • 21岁祭

    不知不觉已经在Very Hard模式下存活了21年了,我俨然升级成为了超级小强,坚强的证明着景德镇工人阶级的生命之伟大,以下省略1WW字……

    P大的事基本搞定了,没啥好担心的了,虽然之前差点纠结致死。总觉得自己有一点犯Jian的味道,总是担心这担心那,显得蹑手蹑脚的,可能还是缺少历练吧。组织的事也算比较圆满的结束了,作为一分子我都积极了近三年了,终于……你懂得。

    彻底把硬盘格了,全盘Linux,一开始折腾的是Arch,觉得有些过于激进,稳定性不够,然后又转向Gentoo了,现在是Gentoo+(VirtualBox+XP),目前使用良好。Sina2WordPress有了雏形,但是又将折腾系统作为借口好久没碰代码了。眼看着假期过去1/3了,看看当初的那些计划,甚感自己的效率低下啊,要好好动手了呢,不能再说那么多空话了。

    英文名改了,也不记得当初为啥选Victor了,一学弟说有点功利,其实我也一直想改的。现在叫Shane了,虽然又有一学弟说是太像Shame了,shame就shame好了,没啥好丢人的……以后我就叫Shane.Hu了,当然你还可以叫我胡旭安。

    怎么说呢,还是觉得自己有点变化的,特别是最近看了几部电影和几本书之后,对古典音乐越来越有感了,但是总觉得自己知识很欠缺了,还望高人指点。

    和她的关系也趋于平稳了,主要也是我不像以前那样瞎纠结了,就这样一起好好走下去吧。

    想找个实习的,但是突然觉得自己好弱啊,各种迷茫……

    想不起来说啥了,我又老了一岁,谢谢没有忘记我生日的人,其他的就不俗套了,就这样。

  • 利用Python解析新浪博客javascript生成的评论

    【前言】sina2wordpress终于有一个大概模样了,目前版本号为0.1
    这是本站项目地址这是代码托管地址

    言归正传,目前新浪博客的评论是用JavaScript生成的,直接用urllib2读取的信息中没有评论信息

    通过Firefox下的firebug或者Chrome自带的Develop Tools,都可以找到js脚本在运行时GET的数据,具体方法是:打开之后选择“Network”标签,并选择“XHR”分类项,刷新页面并等待加载完成,稍等一会就会看到GET了一条以“comment”开头的html页面。

    主要规律是,例如韩寒同学的这篇日志地址是 “http://blog.sina.com.cn/s/blog_4701280b0101854o.html”,那么打开日志页面时GET的就会是 “comment_4701280b0101854o_1.html”,规律不难发现,后面的那个数字就是评论的页数,最后的那个页数可以变为任意整数(没有尝试过上限),当超过实际评论页数时,显示的会是同样的编码,这也就是循环结束条件

    此地址为相对路径,打开后是一堆乱码,这就是JavaScript的数据存储形式——json编码。可以简单的类比成Python中的字典,本例中共有两个关键字,一个是“Code”,对应值”A00006″,没发现啥用处,第二个关键字是“data”,其余部分均为data的对应值,利用Python的json模块进行解析可以发现,这就是评论的html代码。不过这里需要注意的是,第二个关键字“data”缺少双引号,直接解析会报错,需要先进行字符串处理,然后再解析。

    相关代码如下(完整代码可以参见前言中的代码托管地址):
    [python]
    #根据json解析之后的html代码总结出来的各个关键信息的正则表达式
    comment_author_pattern=re.compile(r’(.*?)‘)
    comment_url_pattern=re.compile(r’(.*?)‘)
    comment_time_pattern=re.compile(r’(.*?)‘)
    comment_content_pattern=re.compile(r’

    (.*?)

    ‘, re.S) #这里的re.S很重要

    def commentsAnalyze(key): #key为地址中间的标识性字符串
    num=1 #表示评论的页数
    url=r’http://blog.sina.com.cn/s/comment_%s_%d.html’ %(key, num) #生成json编码对应的地址
    page=urllib2.urlopen(url).read().replace(‘data:’,’”data”:’,1) #给data添加双引号
    while not ‘noCommdate’ in page: #noCommdata是无评论json编码页面的关键字
    data=json.loads(page)[‘data’] #最关键的一部,json代码的解析并有效部分

    #通过正则匹配出相关信息,其中url是可能存在的,将稍后处理
    author=comment_author_pattern.findall(data)
    url=[]
    time=comment_time_pattern.findall(data)
    content=comment_content_pattern.findall(data)

    #后处理url,判断前面提取出来的author周边是不是带有标签(超链接)即可
    for i in range(len(author)):
    comment_id+=1
    result=comment_url_pattern.search(author[i])
    if result:
    url.append(result.group(1))
    author[i]=result.group(2)
    else: url.append(”)

    num+=1 #评论页数+1
    url=r’http://blog.sina.com.cn/s/comment_’+key+’_’+str(num)+’.html’ #生成新的页面地址
    page=urllib2.urlopen(url).read().replace(‘data:’,’”data”:’,1) #修正data的双引号,重复循环
    [/python]

    【后记】感谢 @lqs 学长和 北航开源俱乐部BHOSC的同胞们 在此问题上给予的帮助