【问题标题】:Changing span element on the website - VBA更改网站上的 span 元素 - VBA
【发布时间】:2018-05-30 20:34:34
【问题描述】:

我需要更改网站上的 span 元素,我的意思是我必须更改 pons 网站上的语言。这是我的代码:

Sub www()

    Set objIE = CreateObject("InternetExplorer.Application")

    objIE.Top = 0
    objIE.Left = 0
    objIE.Width = 800
    objIE.Height = 600
    objIE.AddressBar = 0
    objIE.StatusBar = 0
    objIE.Toolbar = 0
    objIE.Visible = True
    objIE.Navigate ("https://pl.pons.com/tłumaczenie-tekstu")

    Do
    DoEvents
    Loop Until objIE.ReadyState = 4
    pagesource = objIE.Document.Body.Outerhtml
    objIE.Document.GetElementsByTagName("TEXTAREA")(0).Value = "piłka"
    objIE.Document.GetElementsByTagName("BUTTON")(5).Click

    Do
    DoEvents
    Loop Until objIE.ReadyState = 4

End Sub

【问题讨论】:

    标签: html vba web-scraping


    【解决方案1】:

    与我之前的 answer 一致,这里是您更改语言的方式。

    注意:

    后期绑定不会为您提供.querySelector 的接口。它必须具有早期绑定,即为HTML Object Library 添加了参考。

    附加参考是Microsoft Internet Controls,但这不会影响.querySelector


    To do from 和 to 语言使用:

    .querySelectorAll("button span")(0).innerText = "bulgarski" '<== To
    .querySelectorAll("button span")(1).innerText = "arabskiego" '<== From
    

    或者更有针对性:

    .querySelectorAll("button[class = ""btn dropdown-toggle""]")(0).innerText = "chinskiego"
    .querySelectorAll("button[class = ""btn dropdown-toggle""]")(1).innerText = "bulgarski"
    

    甚至:

    .querySelectorAll("button.btn.dropdown-toggle span")(0).innerText = "chinskiego"
    .querySelectorAll("button.btn.dropdown-toggle span")(1).innerText = "arabskiego"
    

    上述方法使用 CSS 选择器,即利用页面样式来选择感兴趣的项目。

    querySelectorAll 返回匹配指定 CSS 选择器的所有节点的 NodeList。然后通过索引访问 NodeList 项目,例如0, 1.


    CSS 选择器:

    1. CSS selectors
    2. CSS Selector Reference

    代码:

    Option Explicit
    
    Public Sub GetInfo()
        Dim IE As New InternetExplorer, html As HTMLDocument, translation As String
        Const TRANSLATION_STRING As String = "Hello"
    
        With IE
            .Visible = True
            .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Set html = .document
    
            With html
    
                .querySelectorAll("button span")(0).innerText = "polskiego"
                .querySelectorAll("button span")(1).innerText = "angielskiego"
    
    '            .querySelectorAll("button[class = ""btn dropdown-toggle""]")(0).innerText = "chinskiego"
    '            .querySelectorAll("button[class = ""btn dropdown-toggle""]")(1).innerText = "bulgarski"
    '
    '            .querySelectorAll("button span")(0).innerText = "chinskiego"
    '            .querySelectorAll("button span")(1).innerText = "chinskiego"
    ''
    '            .querySelectorAll("button.btn.dropdown-toggle span")(0).innerText = "chinskiego"
    '            .querySelectorAll("button.btn.dropdown-toggle span")(1).innerText = "arabskiego"
    
    Stop
    
                .querySelector("textarea.text-translation-source.source").Value = TRANSLATION_STRING
                .querySelector("button.btn.btn-primary.submit").Click
                Application.Wait Now + TimeSerial(0, 0, 3)
                translation = .querySelector("div.translated_text").innerText
            End With
    
            Debug.Print translation
            'Quit '<== Remember to quit application
        End With
    
    End Sub
    

    示例运行:


    编辑:

    这并不可靠,但你可以这样做,用于与 HTML 文件的后期绑定

    .getElementsByTagName("span")(0).innerText = "chinskiego"  '<== from
    .getElementsByTagName("span")(26).innerText = "bulgarski" '<== to
    

    后期绑定:

    Option Explicit
    Public Sub GetInfo()
        Dim IE As Object, html As Object
    
        With CreateObject("InternetExplorer.Application")
            .Visible = True
            .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Set html = CreateObject("htmlfile")
            Set html = .document
    
            With html
    
                .getElementsByTagName("span")(0).innerText = "chinskiego"
                .getElementsByTagName("span")(26).innerText = "bulgarski"
    
                Stop
            End With
            .Quit
        End With
    
    End Sub
    

    真的不希望这个答案永远持续下去(因此为当前长度道歉)......作为对 OPs 稍后关于有时在后期绑定翻译中没有保留的值的问题的回应。

    请查看更多脆弱版本。在下面的版本中,我首先循环了所有 span 元素,并找出了哪个索引与哪个语言选择和哪个框相关:

    跨度迷你指南:

    代码:

    Option Explicit
    Public Sub GetInfo()
        Dim IE As Object, html As Object, translation As String
    
        With CreateObject("InternetExplorer.Application")
            .Visible = True
            .navigate "https://pl.pons.com/t%C5%82umaczenie-tekstu"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            Set html = CreateObject("htmlfile")
            Set html = .document
    
            Dim i As Long
            'Dim listOfSpanElements As Worksheet
            'Set listOfSpanElements = Worksheets.Add
    
    '        With listOfSpanElements ''<== This was used to ascertain position of all the span elements and hence which span to click on to select a language.
    '
    '            For i = 0 To html.getElementsByTagName("span").Length - 1
    '                Cells(i + 1, 1) = html.getElementsByTagName("span")(i).innerText
    '            Next i
    '
    '        End With
    
    
            With html
    
                .getElementsByTagName("button")(1).Focus
                .getElementsByTagName("button")(1).Click
                .getElementsByClassName("text-translation-source source")(0).innerText = "Sponsorowane"
                .getElementsByTagName("span")(15).Click 'FROM polskiego
                .getElementsByTagName("span")(47).Click 'TO angielski
                .getElementsByClassName("btn btn-primary submit")(0).Click
                Application.Wait Now + TimeSerial(0, 0, 4)
               Stop
    
                For i = 0 To .getElementsByClassName("text-translation-target target").Length - 1
                   Debug.Print .getElementsByClassName("text-translation-target target")(i).innerText '<==later remove "Trwa ladowanie..."
                Next i
            End With
            .Quit
        End With
    
    End Sub
    

    【讨论】:

    • 这一行: .querySelector("button span").innerText = "polskiego" 有效,但是当我使用这些时,我想从中选择语言: .querySelectorAll("button span")( 0).innerText = "polskiego" .querySelectorAll("button span")(1).innerText = "angielskiego" 我收到一个错误“自动化错误”
    • 我的结果很好。你能告诉我更多关于错误的信息吗?
    • 没关系。我们是来帮忙的。请随时提出问题。我认为 QuerySelector 适用于 IE9+。
    • 好吧,我不懂这个语法。我已经学习 VBA 几个月了,但我根本不懂 HTML。现在我收到一个错误:“对象不支持此属性或方法”
    • 你运行的是什么版本的IE?
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多