編者注:我們發(fā)現(xiàn)了有趣的系列文章《30天學(xué)習(xí)30種新技術(shù)》,正在翻譯,一天一篇更新,年終禮包。下面是第 16 天的內(nèi)容。
今天的“30天學(xué)習(xí)30種新技術(shù)”的主題是如何使用Python進(jìn)行文章提取。這幾個(gè)月來,我對文章提取很感興趣,因?yàn)槲蚁雽懸粋€(gè)Prismatic克隆。Prismatic創(chuàng)建基于用戶興趣的新聞源。提取文章的主要內(nèi)容、圖片和其他元信息對大多數(shù)類似Prismatic的內(nèi)容發(fā)現(xiàn)站點(diǎn)很有用。本文中,我們將學(xué)習(xí)如何使用Python的goose-extractor包來完成這個(gè)任務(wù)。我們首先介紹一些基礎(chǔ)知識,然后使用Goose Extractor 的 API 來開發(fā)一個(gè)簡單的Flask應(yīng)用。

Goose Extractor是什么?
Goose Extractor是一個(gè)Python的開源文章提取庫??梢杂盟崛∥恼碌奈谋緝?nèi)容、圖片、視頻、元信息和標(biāo)簽。Goose本來是由Gravity.com編寫的Java庫,最近轉(zhuǎn)向了scala。
Goose Extractor網(wǎng)站是這么介紹的:
Goose Extractor完全用Python重寫了。目標(biāo)是給定任意資訊文章或者任意文章類的網(wǎng)頁,不僅提取出文章的主體,同時(shí)提取出所有元信息以及圖片等信息。
為什么關(guān)心Goose Extractor
我決定學(xué)習(xí)Goose Extractor是因?yàn)椋?/p>
我打算開發(fā)需要文章提取功能的應(yīng)用。Goose Extractor基于NLTK和Beautiful Soup,分別是文本處理和HTML解析的領(lǐng)導(dǎo)者。
我想了解如何用Python進(jìn)行文章提取。
安裝Goose Extractor
我們首先需要安裝Python和virtualenv,本文使用的Python版本是2.7 。
然后使用如下命令安裝:
mkdir myapp
cd myapp
virtualenv venv --python=python2.7
. venv/bin/activate
pip install goose-extractor
GitHub倉庫
今天的示例程序的代碼可從GitHub取得。
應(yīng)用
示例應(yīng)用運(yùn)行在 OpenShift 上 http://gooseextractor-t20./ 用戶可以提交鏈接,應(yīng)用會(huì)顯示標(biāo)題,主要圖片和正文的前200個(gè)字符。

我們將開發(fā)一個(gè)簡單的REST API Flask應(yīng)用。如果你不了解Flask,你可以看這篇我以前寫的文章。
安裝Flask:
. venv/bin/activate
pip install flask
在myapp 目錄下創(chuàng)建app.py ,內(nèi)容如下:
from flask import Flask, request, render_template,jsonify
from goose import Goose
app = Flask(__name__)
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/api/v1/extract')
def extract():
url = request.args.get('url')
g = Goose()
article = g.extract(url=url)
response = {'title' : article.title , 'text' : article.cleaned_text[:250],'image': article.top_image.src}
return jsonify(response)
if __name__ == "__main__":
app.run(debug=True)
解釋下上面的代碼:
從flask 包導(dǎo)入了Flask 類、request 對象、jsonify 函數(shù)和render_template 函數(shù)。
從goose 包導(dǎo)入Goose 類。
定義了/ 和index 的路由。若用戶向/ 或/index 發(fā)送GET請求,會(huì)渲染index.html 頁面。
定義了/api/v1/extract 路由。我們從請求對象中獲取url ,然后創(chuàng)建了一個(gè)Goose類的實(shí)例。接著提取文章。最后創(chuàng)建一個(gè)json對象并返回該對象。json對象中包括標(biāo)題、文本和主要圖片。
最后我們使用python app.py 命令來啟動(dòng)開發(fā)服務(wù)器,以運(yùn)行應(yīng)用。我們把上面的代碼復(fù)制到app.py文件中。我們同時(shí)通過Debug=True 開啟了調(diào)試,這樣當(dāng)意料之外的情況出現(xiàn)時(shí),瀏覽器就可以提供一個(gè)交互式的調(diào)試器。開啟調(diào)試的另一個(gè)好處是,改動(dòng)文件 之后,服務(wù)會(huì)自動(dòng)重新加載。我們可以讓調(diào)試器在后臺(tái)運(yùn)行,然后繼續(xù)在我們的應(yīng)用上工作。這提供了高效的開發(fā)環(huán)境。
我們將在index.html 中使用Twitter Bootstrap來添加樣式。我們同時(shí)使用了jQuery,以便實(shí)現(xiàn)keyup 事件觸發(fā)REST調(diào)用。退格、制表符、回車、上、下、左、右不會(huì)觸發(fā)REST調(diào)用。
<!DOCTYPE html>
<html>
<head>
<title>Extract Title, Text, and Image from URL</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.css">
<style type="text/css">
body {
padding-top:60px;
padding-bottom: 60px;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">TextExtraction</a>
</div>
</div>
</div>
<div id="main" class="container">
<form class="form-horizontal" role="form" id="myform">
<div class="form-group">
<div class="col-lg-4">
<input type="url" id="url" name="url" class="form-control" placeholder="Url you want to parse" required>
</div>
</div>
<div class="form-group">
<input type="submit" value="Extract" id="submitUrl" class="btn btn-success">
</div>
</form>
</div>
<div id="loading" style="display:none;" class="container">
<img src="/static/images/loader.gif" alt="Please wait.." />
</div>
<div id="result" class="container">
</div>
<script type="text/javascript" src="static/js/jquery.js"></script>
<script type="text/javascript">
$("#myform").on("submit", function(event){
$("#result").empty();
event.preventDefault();
$('#loading').show();
var url = $("#url").val()
$.get('/api/v1/extract?url='+url,function(result){
$('#loading').hide();
$("#result").append("<h4>"+result.title+"</h4>");
$("#result").append("<img src='"+result.image+"' height='300' width='300'</img>");
$("#result").append("<p class='lead'>"+result.text+"</p>");
})
});
</script>
</body>
</html>
你可以從github 倉庫中復(fù)制js和css文件。
上面的HTML文件中,表單提交觸發(fā)REST調(diào)用。當(dāng)我們接受到回應(yīng)后,將它附加到result div中。
部署到云端
在我們部署應(yīng)用到OpenShift之前,我們需要先設(shè)置一下:
注冊一個(gè)OpenShift賬號。注冊是完全免費(fèi)的,Red Hat給每個(gè)用戶三枚免費(fèi)的Gear,可以用Gear運(yùn)行你的應(yīng)用。在寫作此文的時(shí)候,每個(gè)用戶能免費(fèi)使用總共 1.5 GB 內(nèi)存和 3 GB 硬盤空間。
安裝 rhc客戶端工具。rhc 是ruby gem,因此你的機(jī)子上需要裝有 ruby 1.8.7以上版本。 只需輸入 sudo gem install rhc 即可安裝 rhc 。如果你已經(jīng)安裝過了,確保是最新版。運(yùn)行sudo gem update rhc 即可升級。關(guān)于配置rhc命令行工具的詳細(xì)信息,請參考: https://openshift./community/developers/rhc-client-tools-install
使用 rhc 的 setup 命令配置你的 OpenShift 賬號。這個(gè)命令會(huì)幫助你創(chuàng)建一個(gè)命名空間,同時(shí)將你的ssh公鑰上傳至 OpenShift 服務(wù)器。
設(shè)置之后,我們可以通過如下命令創(chuàng)建一個(gè)新的OpenShift應(yīng)用。
rhc create-app day16demo python-2.7 --from-code https://github.com/shekhargulati/day16-goose-extractor-demo.git --timeout 180
這會(huì)為我們創(chuàng)建一個(gè)名為gear的應(yīng)用容器,并自動(dòng)配置相應(yīng)的SELinux政策和cgroup設(shè)置。OpenShift同時(shí)會(huì)為我們創(chuàng)建一個(gè)私有的git倉庫,并將其克隆到本地。最后,OpenShift會(huì)自動(dòng)配置DNS。應(yīng)用可以在如下地址訪問 http://gooseextractor-t20./
好了,這就是今天的內(nèi)容。請不斷反饋。
原文 Day 16: Goose Extractor--An Article Extractor That Just Works
翻譯 SegmentFault
|