我试图将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>当我运行代码时。它只是加载页面,什么也不做。它甚至没有显示任何错误。我尝试过使用"getElementsByClassName"和"getElementbyID"来获取数据
这是我试图从中获取数据的屏幕截图。

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发布于 2019-04-17 17:42:34
如果你没有得到任何错误,那么你一定是在使用一些错误处理来掩盖错误?
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()
你不能把一个对象赋给一个字符串,这应该抛出一个类型不匹配的错误。
https://stackoverflow.com/questions/55722392
复制相似问题