我正在尝试使用Enlive从Clojure中获取HTML链接。我可以从页面获取所有链接的列表吗?我可以迭代它们吗?
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
# <html>
# <head>
# <title>
# The Dormouse's story
# </title>
# </head>
# <body>
# <p class="title">
# <b>
# The Dormouse's story
# </b>
# </p>
# <p class="story">
# Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">
# Elsie
# </a>
# ,
# <a class="sister" href="http://example.com/lacie" id="link2">
# Lacie
# </a>
# and
# <a class="sister" href="http://example.com/tillie" id="link2">
# Tillie
# </a>
# ; and they lived at the bottom of a well.
# </p>
# <p class="story">
# ...
# </p>
# </body>
# </html>
links = soup.find_all('a')
或
links = soup('a')
我怎样才能在Enloive的Clojure中做到这一点?
答案 0 :(得分:1)
这很简单:
(require '[net.cgrand.enlive-html :as enlive])
(let [data (enlive/html-resource (java.net.URL. "https://www.stackoverflow.com"))
all-refs (enlive/select data [:a])]
(first all-refs))
;;=> {:tag :a, :attrs {:href "https://stackoverflow.com", :class "-logo js-gps-track", :data-gps-track "top_nav.click({is_current:true, location:1, destination:8})"}, :content ("\n " {:tag :span, :attrs {:class "-img"}, :content ("Stack Overflow")} "\n ")}
all-refs
集合将包含enlive表示形式的页面中的所有链接。
(let [data (enlive/html-resource (java.net.URL. "https://www.stackoverflow.com"))
all-refs (enlive/select data [:a])]
(map #(-> % :attrs :href) all-refs))
例如,将收集链接中的所有href
值
答案 1 :(得分:1)
首先,您需要使用Enlive的html-resource
函数来摄取一些HTML。我们抓住news.google.com:
(defn fetch-url [url]
(html/html-resource (java.net.URL. url)))
(def goog-news (fetch-url "https://news.google.com"))
要获取所有<a>
代码,请使用select
函数和一个简单的选择器(第二个参数):
(html/select goog-news [:a])
这将评估一系列地图,每个<a>
标签一个。以下是来自今天新闻的示例<a>
代码映射:
{:tag :a,
:attrs {:class "nuEeue hzdq5d ME7ew",
:target "_blank",
:href "https://www.vanityfair.com/hollywood/2018/01/first-black-panther-reviews",
:jsname "NV4Anc"},
:content ("The First Black Panther Reviews Are Here—and They're Ecstatic")}
要获取每个<a>
的内部文字,您可以map
活跃text
对结果的影响,例如(map html/text *1)
。要获得每个href
,您可以(map (comp :href :attrs) *1)
。