更改网站上的span元素 - VBA

时间:2018-05-30 20:34:35

标签: html vba web-scraping

我需要在网站上更改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

1 个答案:

答案 0 :(得分:1)

与我之前的answer一致,这里是你改变语言的方法。

注意:

后期绑定无法为您提供.querySelector的界面。它必须与早期绑定,即为HTML Object Library添加参考。

其他参考资料为Microsoft Internet Controls,但这不会影响.querySelector

要使用以下语言:

.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
  3. <强>代码:

    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
    

    示例运行:

    Test run

    修改

    这不是很强大,但你可以做到,因为后期绑定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
    

    不要真的希望这个答案永远继续下去(所以对当前的长度表示道歉)......为了回应OP后来的问题,有时候在晚期翻译中没有保留价值。

    请参阅更多易碎版本。在下面的版本中,我首先循环所有的span元素,并找出哪个索引与哪个语言选择有关,以及在哪个框中:

    跨度迷你指南:

    span elements with index

    <强>代码:

    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