【发布时间】:2021-01-06 07:37:07
【问题描述】:
使用JSoup用Clojure解析一个html字符串,源码如下
依赖关系
:dependencies [[org.clojure/clojure "1.10.1"]
[org.jsoup/jsoup "1.13.1"]]
源代码
(require '[clojure.string :as str])
(def HTML (str "<html><head><title>Website title</title></head>
<body><p>Sample paragraph number 1 </p>
<p>Sample paragraph number 2</p>
</body></html>"))
(defn fetch_html [html]
(let [soup (Jsoup/parse html)
titles (.title soup)
paragraphs (.getElementsByTag soup "p")]
{:title titles :paragraph paragraphs}))
(fetch_html HTML)
预期结果
{:title "Website title",
:paragraph ["Sample paragraph number 1"
"Sample paragraph number 2"]}
很遗憾,结果不如预期
user ==> (fetch_html HTML)
{:title "Website title", :paragraph []}
【问题讨论】:
-
您是否尝试将“p”而不是“a”传递给 getElementsByTag 方法?
-
能否请您更具体地了解使用的版本等。您的代码WFM。不需要使用
str(导入也不需要)-但这不应该损害结果。 -
@cfrick :dependencies [[org.clojure/clojure "1.10.1"] [org.jsoup/jsoup "1.13.1"]],谢谢
标签: java web-scraping clojure jsoup