Прежде всего, извините за заголовок. Хотя английский sh не является моим родным языком, я даже не знаю, как назвать то, что я пытаюсь сделать, на своем родном языке sh.
Что я пытаюсь сделать это взять входные данные (автоматически генерируемые путем загрузки страницы с curl
, затем преобразованные из HTML в JSON очень грубым способом с использованием pup
) и преобразовать их во что-то, с чем было бы легче работать позже , Входные данные выглядят так:
[
{
"children": [
{
"class": "label label-info",
"tag": "span",
"text": "Lesson"
},
{
"tag": "h2",
"text": "Is That So?"
},
{
"tag": "p",
"text": "Learn how to provide shortened answers with そうです and stay in the conversation with そうですか."
},
{
"class": "btn btn-primary",
"href": "https://www.nihongomaster.com/japanese/lessons/view/62/is-that-so",
"tag": "a",
"text": "Read Lesson"
}
],
"class": "row col-sm-12",
"tag": "div"
},
{
"children": [
{
"class": "label label-warning",
"tag": "span",
"text": "Drills"
},
{
"tag": "h2",
"text": "Yes, That Is So."
},
{
"tag": "p",
"text": "Practice the phrases and vocab from the lesson, Is That So?"
}
],
"class": "row col-sm-12",
"tag": "div"
}
]
И мой желаемый вывод будет извлекать различные значения из массива children
каждого объекта в нечто вроде этого:
[
{
"title": "Is That So?", // <-- in other words, find "tag" == "h2" and output "text" value
"perex": "Learn how to provide shortened answers with そうです and stay in the conversation with そうですか.", // "tag" == "p", "text" value
"type": "lesson", // "tag" == "span", "text" value (lowercased if possible? Not needed though)
"link": "https://www.nihongomaster.com/japanese/lessons/view/62/is-that-so" // "tag" == "a", "href" value
},
{
"title": "Yes, That Is So."
"perex": "Practice the phrases and vocab from the lesson, Is That So?",
"type": "drills",
"link": null // Can be missing!
}
]
Я пробовал различные эксперименты с select
, но не дал ни одного полезного результата, поэтому я не уверен, стоит ли делиться своими попытками.