首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用Excel VBA获取Youtube Trending视频名称和链接

尝试使用Excel VBA获取Youtube Trending视频名称和链接
EN

Stack Overflow用户
提问于 2019-04-17 15:44:04
回答 1查看 159关注 0票数 0

我试图将Youtube Trending视频转换为excel工作表,并提供标题和视频链接。但是我的代码不能工作。我不知道我的代码或趋势页是否有问题。这是我尝试拍摄数据的地方的代码

代码语言:javascript
复制
    <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"来获取数据

这是我试图从中获取数据的屏幕截图。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 17:42:34

如果你没有得到任何错误,那么你一定是在使用一些错误处理来掩盖错误?

result已经被定义为一种String类型--但您正在做的是:

代码语言:javascript
复制
'...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()

你不能把一个对象赋给一个字符串,这应该抛出一个类型不匹配的错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55722392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档