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
只是一個私人幫手函數來獲取屬性內容。 編輯了原始問題並添加了清晰的功能。 –