2011-11-06 4 views
1

사이트를 구문 분석하려고하지만 HTML은 엉망입니다. 구문 분석 사이트에서 더 많은 경험을 가진 사람이 나를 도울 수 있습니까?node.js로 이상한 html 구문 분석

<tr> 
<td><font FACE=Tahoma color='#CC0000' size=2><b>Date</b></font></td>  
<td><font FACE=Tahoma color='#CC0000' size=2><b>Place</b></font></td> 
<td><font FACE=Tahoma color='#CC0000' size=2><b>Situation</b></font></td> 
</tr> 

<tr><td rowspan=2>16/09/2011 10:11</td><td>New York</td><td><FONT COLOR="000000">Situation Red</font></td></tr> 
<tr><td colspan=2>Optional comment hello new york</td></tr> 
<tr><td rowspan=2>16/09/2011 10:08</td><td>Texas</td><td><FONT COLOR="000000">Situation Green</font></td></tr> 
<tr><td colspan=2>Optional comment hello texas </td></tr> 
<tr><td rowspan=1>06/09/2011 13:14</td><td>California</td><td><FONT COLOR="000000">Yellow Situation</font></td></tr> 
</TABLE> 

이상한 미친 것은 또한 시작점 (캘리포니아) 코멘트를 해달라고하지 테이블의 머리에있는 주석입니다. 그래서, 이렇게 될 것입니다 항상 시작점 :

날짜 : 2011년 6월 9일 13시 14분

장소 : 캘리포니아

상황 : 노란색 상황

댓글 : null

다른 모든 장소는 의견을 가지고 다음과 같이 될 것입니다 :

날짜 : 16/09/2011 10시 11분

장소 : 뉴욕

상황 : 상황 레드

댓글 : 선택적 hello new york.

몇 가지 접근법을 시도했지만, node.js에 대한 경험이 많지 않고 HTML 구문 분석에 대한 경험이 적습니다. 나는 미친 것들을 파싱하기 시작해야한다.

+2

시도 : HTTPS : (tidy.js로 저장하는 경우)

var spawn = require('child_process').spawn; var fs = require('fs'); var tidy = (function() { this.html = function(str, callback) { var buffer = ''; var error = ''; if (!callback) { throw new Error('No callback provided for tidy.html'); } var ptidy = spawn( 'tidy', [ '--quiet', 'y', '--force-output', 'y', '--bare', 'y', '--break-before-br', 'y', '--hide-comments', 'y', '--output-xhtml', 'y', '--fix-uri', 'y', '--wrap', '0' ]); ptidy.stdout.on('data', function (data) { buffer += data; }); ptidy.stderr.on('data', function (data) { error += data; }); ptidy.on('exit', function (code) { //fs.writeFileSync('last_tidy.html', buffer, 'binary'); callback(buffer); }); ptidy.stdin.write(str); ptidy.stdin.end(); } return this; })(); module.exports = tidy; 

예 : 여기

은 깔끔한 통해 HTML을 실행하는 모듈이다 //github.com/tautologistics/node-htmlparser – mikeycgto

답변

10

node.js에 분산 스크레이퍼를 만들었습니다. html을 구문 분석하는 것이 HTML 깔끔하게 처리되었다는 것을보다 쉽게 ​​발견했습니다.

require('./tidy.js'); 
tidy.html('<table><tr><td>badly formatted html</tr>', function(html) { console.log(html); }); 

결과 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" /> 
<title></title> 
</head> 
<body> 
<table> 
<tr> 
<td>badly formatted html</td> 
</tr> 
</table> 
</body> 
</html> 
+1

require ('htmltidy/htmltidy.js')를 사용하여이 모듈을 사용할 수있었습니다. 다른 사람이 npm에서 다운로드 :-) – netpoetica