6

我是clojure的新手,我需要一些例子。请告诉我如何使用 clojure 解析 html 文件?

4

3 回答 3

17

Enlive is a great tool for this. In short:

(ns foo.bar
  (:require [net.cgrand.enlive-html :as html]))

(defn fetch-page [url]
  (html/html-resource (java.net.URL. url)))

Here is a nice tutorial on using it both as a scraper/parser and as a template engine:

Here is a short example of scraping a page.

Another option is clj-tagsoup. Enlive also uses tagsoup, but in addition has a pluggable parser so you can add support for other parsers.

于 2013-03-18T10:50:59.480 回答
4

Clojure's xml parsing library is there for you.

Parses and loads the source s, which can be a File, InputStream or String naming a URI. Returns a tree of the xml/element struct-map, which has the keys :tag, :attrs, and :content. and accessor fns tag, attrs, and content. Other parsers can be supplied by passing startparse, a fn taking a source and a ContentHandler and returning a parser

Or use enlive, it's framework fully on clojure or use Java based HtmlCleaner.

于 2013-03-18T10:58:43.183 回答
1

HTML 解析器

来源 - https://www.clojure-toolbox.com

于 2020-10-28T19:09:06.790 回答