Как сделать XPATH запрос с несколькими элементами?
Загружаю https://www.youtube.com/results?search_query=music
Пытаюсь с помощью XPATH запроса //h3[@class='title-and-badge style-scope ytd-video-renderer'] найти строку <a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" title="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist" href="/watch?v=E4KMnaOF7lU" aria-label="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views"> Но находит только (см скрин): https://prnt.sc/1ip3ou8 Как изменить XPATH чтоб находил в выдаче все элементы и чтоб в каждом было сразу title="" href="" aria-label="" и желательно чтоб ставил между ними разделитель, например такой @@@ . Должно получится что-то вроде этого: Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist @@@ /watch?v=E4KMnaOF7lU @@@ Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views |
mik888em,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
body{
font-weight: bold;
}
</style>
</head>
<body>
<a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" title="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist" href="/watch?v=E4KMnaOF7lU" aria-label="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views"></a>
<script>
let elem = document.querySelector('#video-title');
attr = 'title,href,aria-label'.split(',').map(key => elem.getAttribute(key)).join(' @@@ ');
document.body.append(attr)
</script>
</body>
</html>
|
Цитата:
<a
id="video-title"
class="yt-simple-endpoint style-scope ytd-video-renderer"
title="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist"
href="/watch?v=E4KMnaOF7lU"
aria-label="Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views"
>
Test
</a>
<script>
const o = document.evaluate('//a[@id="video-title"] ', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );
const arr = []
arr.push(o.singleNodeValue.title)
arr.push(o.singleNodeValue.href)
arr.push(o.singleNodeValue.ariaLabel)
alert(arr.join('\n'));
</script>
|
Я сначала сохраняю код страницы поиска ютуба https://www.youtube.com/results?search_query=music в переменную [[KOD]]
Подскажите, а как применить этот JS-код к этой переменной [[KOD]], На странице много видео, и JS-код должен для каждого видео собрать результат, и получится что-то вроде этого: Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist @@@ /watch?v=E4KMnaOF7lU @@@ Best songs to boost your mood ~ Chill vibes ???? Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views Metallica - Live in Berlin, Germany (2019) [Full Webcast] [AUDIO UPGRADE] @@@ /watch?v=s_hYeCZo2Nw @@@ Metallica - Live in Berlin, Germany (2019) [Full Webcast] [AUDIO UPGRADE] by metfan4l 1 year ago 2 hours, 26 minutes 4,829,409 views и так далее вниз спаршеное из [[KOD]] |
mik888em,
цикл по всем "[id='video-title']" |
| Часовой пояс GMT +3, время: 21:07. |