如何从 Racket 中的 html 中提取元素?

How to extract element from html in Racket?

我想提取reddit中的url,我的代码是

#lang racket

(require net/url)
(require html)

(define reddit (string->url "http://www.reddit.com/r/programming/search?q=racket&sort=relevance&restrict_sr=on&t=all"))
(define in (get-pure-port reddit #:redirections 5))

(define response-html (read-html-as-xml in))
(define content-0 (list-ref response-html 0))

(close-input-port in)

上面的content-0是

(element
 (location 0 0 15)
 (location 0 0 82)
...

我想知道如何从中提取特定内容。

  1. 通常将HTML作为x-expressions处理比html模块的struct更方便。

  2. 此外,您可能应该使用 call/input-url 来处理自动关闭端口。

您可以通过定义一个 read-html-as-xexpr 函数并像这样使用它来结合这两种想法:

#lang racket/base

(require html
         net/url
         xml)

(define (read-html-as-xexpr in) ;; input-port? -> xexpr?
  (caddr
   (xml->xexpr
    (element #f #f 'root '()
             (read-html-as-xml in)))))

(define reddit (string->url "http://www.reddit.com/r/programming/search?q=racket&sort=relevance&restrict_sr=on&t=all"))

(call/input-url reddit
                get-pure-port
                read-html-as-xexpr)

这将 return 一个大的 x 表达式,例如:

'(html
  ((lang "en") (xml:lang "en") (xmlns "http://www.w3.org/1999/xhtml"))
  (head
   ()
   (title () "programming: search results")
   (meta
    ((content " reddit, reddit.com, vote, comment, submit ")
     (name "keywords")))
   (meta
    ((content "reddit: the front page of the internet") (name "description")))
   (meta ((content "origin") (name "referrer")))
   (meta ((content "text/html; charset=UTF-8") (http-equiv "Content-Type")))
... snip ...

如何提取其中的特定片段?

  • 对于我不希望整体结构发生变化的简单 HTML,我通常只使用 match.

  • 然而,更正确和可靠的方法是使用 xml/path module.



更新:我注意到你的问题是从询问提取 URL 开始的。这是更新为使用 se-path*/list 获取所有 <a> 元素的所有 href 属性的示例:

#lang racket/base

(require html
         net/url
         xml
         xml/path)

(define (read-html-as-xexprs in) ;; (-> input-port? xexpr?)
  (caddr
   (xml->xexpr
    (element #f #f 'root '()
             (read-html-as-xml in)))))

(define reddit (string->url "http://www.reddit.com/r/programming/search?q=racket&sort=relevance&restrict_sr=on&t=all"))

(define xe (call/input-url reddit
                           get-pure-port
                           read-html-as-xexprs))

(se-path*/list '(a #:href) xe)

结果:

'("#content"
  "http://www.reddit.com/r/announcements/"
  "http://www.reddit.com/r/Art/"
  "http://www.reddit.com/r/AskReddit/"
  "http://www.reddit.com/r/askscience/"
  "http://www.reddit.com/r/aww/"
  "http://www.reddit.com/r/blog/"
  "http://www.reddit.com/r/books/"
  "http://www.reddit.com/r/creepy/"
  "http://www.reddit.com/r/dataisbeautiful/"
  "http://www.reddit.com/r/DIY/"
  "http://www.reddit.com/r/Documentaries/"
  "http://www.reddit.com/r/EarthPorn/"
  "http://www.reddit.com/r/explainlikeimfive/"
  "http://www.reddit.com/r/Fitness/"
  "http://www.reddit.com/r/food/"
  ... snip ...