我正在尝试用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>
^{pr2}$
或者
links = soup('a')
在Clojure和Enlive中如何做到这一点?在
首先需要使用Enlive的
html-resource
函数摄取一些HTML。我们会抓住的新闻谷歌公司名称:要获取所有的
^{pr2}$<a>
标记,请将select
函数与一个简单的选择器一起使用(第二个参数):这将计算为一个映射序列,每个
<a>
标记一个。下面是一个来自今日新闻的<a>
标记映射的示例:要获得每个
<a>
的内部文本,可以使用map
Enlive的text
函数来处理结果,例如(map html/text *1)
。要获得每个href
,可以(map (comp :href :attrs) *1)
。在这很简单:
^{pr2}$all-refs
集合将以生动的表示形式包含来自page的所有链接。在例如,将从链接收集所有
href
值相关问题 更多 >
编程相关推荐