gpt4 book ai didi

Clojure,使用 Enlive 从选择器中提取原始 HTML?

转载 作者:行者123 更新时间:2023-12-02 17:03:20 27 4
gpt4 key购买 nike

我需要从 HTML 页面的特定部分检索一些原始 HTML

我编写了抓取器,它抓取了适当的 div,但它返回了标签映射。

(:use [net.cgrand.enlive-html :as html])

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

(defn parse-test []
(let [url "http://www.ncbi.nlm.nih.gov/pubmedhealth/PMH0000928/"
url-data (fetch-url url)
id "a693025"]
(first (html/select url-data [(keyword (str "#" id "-why"))]))))

输出:

{:tag :div, :attrs {:class "section", :id "a693025-why"}, :content ({:tag :h2, :attrs nil, :content ({:tag :span, :attrs {:class "title"}, :content ("Why is this medication prescribed?")})} {:tag :p, :attrs nil, :content ("Zolpidem is used to treat insomnia (difficulty falling asleep or staying asleep). Zolpidem belongs to a class of medications called sedative-hypnotics. It works by slowing activity in the brain to allow sleep.")})}

如何将其转换为原始 html?我找不到任何 enlive 函数来执行此操作。

最佳答案

(apply str (html/emit* [(parse-test)]))
; => "<div class=\"section\" id=\"a693025-why\"><h2><span class=\"title\">Why is this medication prescribed?</span></h2><p>Zolpidem is used to treat insomnia (difficulty falling asleep or staying asleep). Zolpidem belongs to a class of medications called sedative-hypnotics. It works by slowing activity in the brain to allow sleep.</p></div>"

关于Clojure,使用 Enlive 从选择器中提取原始 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com