1

我创建了一个 vba 脚本来解析不同的title帖子以及editing status来自网站的那些帖子。我现在想做的是让我的脚本title从它的登录页面解析 ,但title在打印editing status. 我不想为此任务创建两个潜艇。我什至不知道在vba中是否可能。但是,如果有任何不清楚的地方,请查看我脚本中的评论。

Sub ImportTitleFromAnotherLocation()
    Const LINK$ = "https://stackoverflow.com/questions/tagged/web-scraping"
    Const prefix$ = "https://stackoverflow.com"
    Dim Http As New XMLHTTP60, Html As New HTMLDocument
    Dim editInfo As Object, I&, targetUrl$, postTile$

    With Http
        .Open "GET", LINK, False
        .send
        Html.body.innerHTML = .responseText
    End With

    With Html.querySelectorAll(".summary .question-hyperlink")
        For I = 0 To .Length - 1

            postTitle = .item(I).innerText 'I like this line to be transferred to the location below

            targetUrl = Replace(.item(I).getAttribute("href"), "about:", prefix)
            With Http
                .Open "GET", targetUrl, False
                .send
                Html.body.innerHTML = .responseText
            End With

            R = R + 1: Cells(R, 1) = postTitle 'here I wish to use the above line like this

            Set editInfo = Html.querySelector(".user-action-time > a")
            If Not editInfo Is Nothing Then
                Cells(R, 2) = editInfo.innerText
            End If
        Next I
    End With
End Sub
4

1 回答 1

1

您正在循环中覆盖您的 html 文档。一种简单的方法是使用第二个 htmldocument 变量。更详细的方法是在循环之前存储标题,例如在附加循环期间存储在数组中,然后使用您的 i 变量对其进行索引以在现有循环期间检索每个标题。

Sub ImportTitleFromAnotherLocation()
    Const LINK$ = "https://stackoverflow.com/questions/tagged/web-scraping"
    Const prefix$ = "https://stackoverflow.com"
    Dim Http As New XMLHTTP60, Html As New HTMLDocument, Html2 As New HTMLDocument

    Dim editInfo As Object, I&, targetUrl$, postTile$
    Dim postTitle As String, r As Long
    With Http
        .Open "GET", LINK, False
        .send
        Html.body.innerHTML = .responseText
    End With

    With Html.querySelectorAll(".summary .question-hyperlink")
        For I = 0 To .Length - 1
            postTitle = .item(I).innerText 'I like this line to be transferred to the location below
            targetUrl = Replace$(.item(I).getAttribute("href"), "about:", prefix)

            With Http
                .Open "GET", targetUrl, False
                .send
                Html2.body.innerHTML = .responseText
            End With

            r = r + 1: ActiveSheet.Cells(r, 1) = postTitle 'here I wish to use the above line like this

            Set editInfo = Html2.querySelector(".user-action-time > a")
            If Not editInfo Is Nothing Then
                ActiveSheet.Cells(r, 2) = editInfo.innerText
            End If
        Next I
    End With
End Sub
于 2019-06-22T20:38:44.050 回答