Попытка получить название видео и ссылки на Youtube Trending с помощью Excel VBA - PullRequest
0 голосов
/ 17 апреля 2019

Я пытаюсь загрузить видео с YouTube Trending в лист Excel с заголовками и ссылками на видео. Но мой код не работает. Я не знаю, что-то не так с моим кодом или страницей трендов. Это код, с которого я пытаюсь записать на видео данные

    <a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" aria-label="VIDEO TITLE & DETAILS" title="VIDEO TITLE" href="/watch?v=J2UuYHahPMI">VIDEO TITLE</a>

Когда я запускаю код. Он просто загружает страницу и ничего не делает. Это даже не показывает никакой ошибки. Я попытался получить данные "getElementbyID", а также с "getElementsByClassName"

Вот скриншот, с которого я пытаюсь получить данные. enter image description here

Sub YOUTUBETREND()

Dim objIE As SHDocVw.InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.youtube.com/feed/trending/"

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'the first search result will go in row 2
y = 2

'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.document.getElementsByClassName("yt-simple-endpoint")

    '...get the href link and print it to the sheet in col C, row y
    result = aEle
    Sheets("Sheet1").Range("C" & y).Value = result

    '...get the text within the element and print it to the sheet in col D
    Sheets("Sheet1").Range("D" & y).Value = aEle.innerText
    Debug.Print aEle.innerText

    'increment our row counter, so the next result goes below
    y = y + 1

'repeat times the # of ele's we have in the collection
Next
objIE.Quit

1 Ответ

0 голосов
/ 17 апреля 2019

Если вы не получаете никаких ошибок, то вы должны использовать некоторую обработку ошибок, которая маскирует ошибку?

result определен как тип String, но вы делаете:

'...get the href link and print it to the sheet in col C, row y
result = aEle
Sheets("Sheet1").Range("C" & y).Value = result

здесь, aEle - это объект из HTMLCollection, потому что вы использовали getElementsByClassName()

Вы не можете присвоить объект строке, это должно привести к ошибке несоответствия типов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...