我試圖抓取node.js web應用程序中網站的描述。它似乎是完美的工作,但是node.js似乎與NCR字符有關(http://en.wikipedia.org/wiki/Numeric_character_reference)。我對鏈路抓取器的代碼如下所示使用tagsoup時出現撇號錯誤
getInfo:(url) ->
errorMessage = 'Invalid Link'
request(url, (error, response, body)->
if (!error && response.statusCode == 200)
handler = new htmlparser.DefaultHandler((err, dom) ->
if (err)
res(error: errorMessage)
else
imgs = select(dom, 'img')
titletags = select(dom,'title')
descripTags = select(dom,'meta')
filteredTags = _.filter(descripTags,(tag) -> tag.attribs.name? && tag.attribs.name == 'description')
uri = response.request.uri.href
mapFunc =(imgSrc) ->
pattern = /^((http|https|ftp):\/\/)/
img = imgSrc.attribs.src
if (!pattern.test(img)) then uri.substring(0,uri.length-1) + img else img
res(
images: _.filter(_.map(imgs,mapFunc),(img)-> (img != '')) || []
title: titletags[0].children[0].raw || ''
description: if filteredTags.length != 0 then filteredTags[0].attribs.content || '' else ''
)
)
parser = new htmlparser.Parser(handler)
parser.parseComplete(body)
else
res(error: errorMessage)
)
作爲一個例子,如果我把在以下網址抓住信息形式(http://www.zdnet.com),描述將ZDNet's breaking news, analysis, and research keeps business technology professionals in touch with the latest IT trends, issues and events.
。撇號是問題(被表示爲'
)
我的問題是,爲什麼沒有任何庫正確解析有效的HTML NCR並將它們轉換爲字符串等效,並且如果沒有辦法解決這個問題,是否可以安全地使用其他庫來替換所有出現的NCR?
我使用的庫下面
request = require 'request'
htmlparser = require 'htmlparser'
select = require('soupselect').select
_ = require 'underscore'