1
我有一个函数,它解析一个html body以获得如下的Open Graph属性。如何在这种情况下使用Elixir Stream处理多个项目?
我不知道如何使用流,以便解析只能完成一次 - 这甚至有可能吗?
def og(body) do
image = attribute_content(body, "meta[property=og:image]")
title = attribute_content(body, "meta[property=og:title]")
site_name = attribute_content(body, "meta[property=og:site_name]")
desc = attribute_content(body, "meta[property=og:description]")
type = attribute_content(body, "meta[property=og:type]")
url = attribute_content(body, "meta[property=og:url]")
author = attribute_content(body, "meta[name=author]")
%{image: image, title: title, type: type,
site_title: site_title, url: url, site_name: site_name,
description: desc, author: author}
end
@doc """
Parse html body for the target element and return the content.
"""
defp attribute_content(body, target) do
Floki.find(body, target) |> Floki.attribute("content") |> List.first
end
什么是'attribute_content'? – Dogbert
只是一个私人帮手函数来获取属性内容。 编辑了原始问题并添加了清晰的功能。 –