打印标题从一个位置导入到另一个位置
Print title importing from one location to another
我创建了一个 vba 脚本来解析不同帖子的 title
以及来自网站的那些帖子的 editing status
。我现在想做的是让我的脚本从它的着陆页解析 title
,但在打印 editing status
的同时打印 title
。我不想为此任务创建两个潜艇。我什至不知道在 vba 中是否可行。但是,如果有任何不清楚的地方,请查看我的脚本中的评论。
Sub ImportTitleFromAnotherLocation()
Const LINK$ = "https://whosebug.com/questions/tagged/web-scraping"
Const prefix$ = "https://whosebug.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
您正在循环覆盖 html 文档。一种简单的方法是使用第二个 htmldocument 变量。更详细的方法是在循环之前存储标题,例如在附加循环期间存储在数组中,然后使用您的 i 变量对其进行索引以在现有循环期间检索每个标题。
Sub ImportTitleFromAnotherLocation()
Const LINK$ = "https://whosebug.com/questions/tagged/web-scraping"
Const prefix$ = "https://whosebug.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
我创建了一个 vba 脚本来解析不同帖子的 title
以及来自网站的那些帖子的 editing status
。我现在想做的是让我的脚本从它的着陆页解析 title
,但在打印 editing status
的同时打印 title
。我不想为此任务创建两个潜艇。我什至不知道在 vba 中是否可行。但是,如果有任何不清楚的地方,请查看我的脚本中的评论。
Sub ImportTitleFromAnotherLocation()
Const LINK$ = "https://whosebug.com/questions/tagged/web-scraping"
Const prefix$ = "https://whosebug.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
您正在循环覆盖 html 文档。一种简单的方法是使用第二个 htmldocument 变量。更详细的方法是在循环之前存储标题,例如在附加循环期间存储在数组中,然后使用您的 i 变量对其进行索引以在现有循环期间检索每个标题。
Sub ImportTitleFromAnotherLocation()
Const LINK$ = "https://whosebug.com/questions/tagged/web-scraping"
Const prefix$ = "https://whosebug.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