2010-02-28 7 views
5

J'aimerais extraire des informations d'une page Web contenue dans un tableau HTML <>. Comment puis-je extraire toutes les informations de la table dans un joli | fichier séparé?Grattage d'une table HTML dans Common Lisp?

 
Author|Book|Year|Comments 
Bill Bryson|Short History of Nearly Everything|2004 
Stephen Hawking|A Brief History of Time|1998|Still haven't read. 

Idéalement, je voudrais avoir une fonction qui prend un fichier URL et la sortie en tant que paramètres donne alors la sortie ci-dessus.

 
(defun extract-table (url filename) 
     (extract-from-html-table (fetch-web-page url))) 

(extract-table "http://www.mypage.com" "output.txt") 

entrée de l'échantillon HTML pour la sortie ci-dessus:

 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> 
<html> 
<head> 
<title>Lisp</title> 
</head> 
<body> 
<h1>Welcome to Lisp</h1> 
<table class="any" style="font-size: 14px;"> 
    <TR class="header"> 
    <td>Author</td> 
    <TD>Book</TD> 
    <td>Year</td> 
    <td>Comments</td> 
    </TR> 
    <tr class="odd"> 
    <td>Bill Bryson</td> 
    <td>Short History of Nearly Everything</td> 
    <td>2004</td> 
    </tr> 
    <tr> 
    <td>Stephen Hawking</td> 
    <td>A Brief History of Time</td> 
    <td>1998</td> 
    <td>Still haven't read.</td> 
    </tr> 
</table> 
</body> 
</html> 

Répondre

7

Commencez par Drakma pour aller chercher les données. Pour analyser la chose, vous pourriez trouver cxml utile. Ou mieux encore: vous pouvez utiliser closure-html, qui devrait analyser arbitrairement HTML 4. La page Common-Lisp.net du paquet closure-html a un screen scraping example.