Ошибка getbyclassname VBA Web Scraping от поисковой системы - PullRequest
0 голосов
/ 27 сентября 2018

Я получаю сообщение об ошибке «объект не поддерживает это свойство или метод» при использовании getelementsbyClassName.Я хочу получить все URL-адреса веб-страниц, которые получены с помощью поисковой системы Baidu (Baidu.com).Но я не смог «нажать» следующую страницу поисковика.Я пытался в течение нескольких дней, было бы хорошо, если бы кто-то мог мне помочь.Спасибо!

Option Explicit

Public Sub GettingURL()

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    Dim c As Long, u As Long
    c = 3
    Dim e
    Dim PageCount As Long

    With ie
        .Visible = False
        .navigate "http://www.baidu.com"

        'searching for the word on the search enigne
        While ie.Busy Or ie.readyState < 4: DoEvents: Wend
        .document.getElementsByTagName("INPUT")("wd").Value  = Sheets("Data").Cells(1, 2).Value
        .document.getElementById("su").Click

        'getting result for page 1 to 20
        For PageCount = 1 To 20

            'loop until the end to get resulting url and navigate to it
            Do
            Loop Until .readyState = READYSTATE_COMPLETE
            .navigate .document.URL

            'navigating page and this line causes problem:
            .document.GetelementByClassName("pc")(0) = PageCount

            'getting the url of all the search result in the result page
            While ie.Busy Or ie.readyState < 4: DoEvents: Wend
            With .document.querySelectorAll("#content_left h3 [href]")
                For u = 0 To 50
                    Sheets("Data").Cells(c, 1) = .Item(u)
                    c = c + 1
                Next
            End With

            PageCount = PageCount + 1
        Next

        .Quit
    End With

End Sub

1 Ответ

0 голосов
/ 27 сентября 2018

Источник вашего сообщения об ошибке - отсутствует "s" в методе:

GetelementByClassName

Это должно быть getElementsByClassName, так как он возвращает коллекцию.


ВыМожно нажать следующую кнопку с помощью

ie.document.querySelector("a.n").Click

a.n - это комбинация селектора CSS для элемента тега a с именем класса n.Первое совпадение возвращается методом querySelector.

Вы можете увидеть целевой элемент здесь:

enter image description here

И селектор CSSздесь:

enter image description here


Вы можете просмотреть все страницы следующим способом:

Option Explicit
Public Sub GettingURL()
    Dim i As Long
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://baidu.com/s?wd=Red"
        While .Busy Or .readyState < 4:  DoEvents:  Wend

        Dim aNodeList As Object: Set aNodeList = .document.querySelectorAll(".pc")

        For i = 1 To aNodeList.Length - 1
           aNodeList.item(i).Click
           While .Busy Or .readyState < 4:  DoEvents:  Wend
           Set aNodeList = .document.querySelectorAll(".pc")
        Next
        Stop '<== Delete me later
        .Quit
    End With
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...