简体   繁体   English

使用 Excel VBA 从网站(HTML div 类)中提取值

[英]Pull value from website (HTML div class) using Excel VBA

I'm trying to automate going to a website and pulling the ratings from several apps.我正在尝试自动访问网站并从多个应用程序中提取评级。

I've figured out how to navigate and login to the page.我已经找到了如何导航和登录页面的方法。

How do I pull the element - the number "3.3" in this case - from this specific section into Excel.我如何将元素(在本例中为数字“3.3”)从该特定部分拉入 Excel。

Being unfamiliar with HTML in VBA, I got this far following tutorials/other questions.由于不熟悉 VBA 中的 HTML,我在教程/其他问题之后得到了这么远。

Rating on website and the code behind it网站评级及其背后的代码
在此处输入图片说明

Sub PullRating()
    
    Dim HTMLDoc As HTMLDocument
    Dim ie As InternetExplorer
    
    Dim oHTML_Element As IHTMLElement
    Dim sURL As String
    
    On Error GoTo Err_Clear
    sURL = "https://www.appannie.com/account/login/xxxxxxxxxx"
    Set ie = New InternetExplorer
    ie.Silent = True
    ie.navigate sURL
    ie.Visible = True
    
    Do
        'Wait until the Browser is loaded
    Loop Until ie.readyState = READYSTATE_COMPLETE
    
    Set HTMLDoc = ie.Document
    
    HTMLDoc.all.Email.Value = "xxxxxxxxx@xxx.com"
    HTMLDoc.all.Password.Value = "xxxxx"
    
    For Each oHTML_Element In HTMLDoc.getElementById("login-form")
        If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
    Next
           
    Dim rating As Variant
    Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
    Range("A1").Value = rating
           
    'ie.Refresh 'Refresh if required
    Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If
    
End Sub

The code below will let you extract text from first element with class name "rating-number ng-binding" in HTML document.下面的代码将让您从 HTML 文档中类名为“rating-number ng-binding”的第一个元素中提取文本。 By the way GetElementsByClassName is supported since IE 9.0.顺便说一下,从 IE 9.0 开始支持 GetElementsByClassName。 I use coding compatible also with older versions in my example.在我的示例中,我使用的编码也与旧版本兼容。

Dim htmlEle1 as IHTMLElement

For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
   If htmlEle1.className = "rating-number ng-binding" then
      Range("A1").Value = htmlEle1.InnerText
      Exit For
   End if
Next htmlEle1

While Ryszards code should do the trick if you want to use the code you have already written then here is the alterations I believe you need to make.如果您想使用已经编写的代码,Ryszards 代码应该可以解决问题,但这里是我认为您需要进行的更改。

For Each oHTML_Element In HTMLDoc.getElementById("login-form")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE

Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml

拉网址形式<div id="text_translate"><p>团队</p><p>我的问题是,如何让 VBA 只获取“href URL 链接列表”?</p><p> 这是我的代码</p><pre>Set Attach = Dr.FindElementsByXPath("//[@id='Order']") Set aList = Dr.FindElementsByCss("#Order [title]") X = 1 to 10 With Sheet2 For i = 1 To X Sheet2.Cells(Lrow,1) = aList.item(i).Text Lrow = Lrow + 1 Next End With Next ' do some stuff End Sub</pre><pre class="lang-html prettyprint-override"> <div class="attachments-files-list" id="Order"> <div class="link"> <a href="#" class="btn btn-default" data-bind="click: downloadAttachments, visible: $root.attachmentsViewModel.attachments().length > 0"> Click <strong data-bind="text:$root.attachmentsViewModel.attachments().length">2</strong> Links </a> </div> <div class="link"> <a href="#" class="js-app-action-el" data-bind="click: $root.attachmentsViewModel.showDeleteAttachmentDialog"> </a> <a href="https://URL/attachmentId=201803%2f93b14b2a-1c10-40f6-a3f6-0e83eea67bf2-check_out_2018.03.09_22%3a16%3a53.jpg&amp;overridefilename=check_out_2018.03.09_22%3a16%3a53.jpg&amp;overrideFileName=check_out_2018.03.09_22%3a16%3a53.jpg" class="js-app-action-el" data-bind="text:fileName, attr: { href: url, title: description }, style: { 'padding-left': $parent.isOutsourced()? 30: 0 }" target="_blank" title="check_out_2018.03.09_22:16:53.jpg" style="padding-left: 0px;"> order_found.jpg </a> </div> </div></pre><p> 让我知道是否需要任何其他信息。 谢谢您的帮助!</p></div> - Pull urls form <a href element from div class using VBA

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拉网址形式<div id="text_translate"><p>团队</p><p>我的问题是,如何让 VBA 只获取“href URL 链接列表”?</p><p> 这是我的代码</p><pre>Set Attach = Dr.FindElementsByXPath("//[@id='Order']") Set aList = Dr.FindElementsByCss("#Order [title]") X = 1 to 10 With Sheet2 For i = 1 To X Sheet2.Cells(Lrow,1) = aList.item(i).Text Lrow = Lrow + 1 Next End With Next ' do some stuff End Sub</pre><pre class="lang-html prettyprint-override"> <div class="attachments-files-list" id="Order"> <div class="link"> <a href="#" class="btn btn-default" data-bind="click: downloadAttachments, visible: $root.attachmentsViewModel.attachments().length > 0"> Click <strong data-bind="text:$root.attachmentsViewModel.attachments().length">2</strong> Links </a> </div> <div class="link"> <a href="#" class="js-app-action-el" data-bind="click: $root.attachmentsViewModel.showDeleteAttachmentDialog"> </a> <a href="https://URL/attachmentId=201803%2f93b14b2a-1c10-40f6-a3f6-0e83eea67bf2-check_out_2018.03.09_22%3a16%3a53.jpg&amp;overridefilename=check_out_2018.03.09_22%3a16%3a53.jpg&amp;overrideFileName=check_out_2018.03.09_22%3a16%3a53.jpg" class="js-app-action-el" data-bind="text:fileName, attr: { href: url, title: description }, style: { 'padding-left': $parent.isOutsourced()? 30: 0 }" target="_blank" title="check_out_2018.03.09_22:16:53.jpg" style="padding-left: 0px;"> order_found.jpg </a> </div> </div></pre><p> 让我知道是否需要任何其他信息。 谢谢您的帮助!</p></div> - Pull urls form <a href element from div class using VBA 如何使用Excel / VBA通​​过Element ID从外部网站获取价值? - How do I pull value from external website by Element ID with Excel / VBA? 使用VBA从网站中提取div类信息 - Extracting div class information from website with VBA 如何使用 html 和 VBA 从网站获取值? - How to get a value from a website using html and VBA? 使用 Excel VBA 将值传递给网站中的文本框 - Pass value to textbox in website using Excel VBA 尝试使用Excel VBA将值从剑道弹出窗口中拉出 - Trying to pull a value out of a kendo popup window using Excel VBA 如何使用vba从网站上提取数据 - How can I pull data from website using vba 使用excel vba访问ASX网站股息表的html元素 - accessing html elements of ASX website dividend table using excel vba 无法从网站上提取HTML - Unable to pull HTML from website 使用excel vba更改网站上的下拉菜单的值2 - Using excel vba to change the value of a dropdown menu on a website2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM