我正在尝试让Excel从搜索引擎中检索数据(torrentz.eu/search?q=abc)
应该获取第一个链接的这些信息并将其显示在Excel上:
Cell A1:MyQuery
单元格A2:LinkTitle
单元格A3:网址
Cell A4:LinkDate。
似乎我无法使用getelementbyid,因为该页面几乎不使用id标记。
到目前为止我所拥有的:
Sub XMLHTTP()
Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object
Dim start_time As Date
Dim end_time As Date
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cookie As String
Dim result_cookie As String
start_time = Time
Debug.Print "start_time:" & start_time
For i = 2 To lastRow
url = "http://www.torrentz.eu/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set objResultDiv = html.getElementBy????
Set objH3 = objResultDiv.getElementBytagname("d1")(0)
Set link = objH3.getelementsbytagname("/d1")(0)
str_text = Replace(link.innerHTML, "<dt>", "")
str_text = Replace(str_text, "</dt>", "")
Cells(i, 2) = str_text
Cells(i, 3) = link.href
Cells(i, 4) = link.date????
DoEvents
Next
end_time = Time
Debug.Print "end_time:" & end_time
Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub
答案 0 :(得分:1)
如果我理解你的后续行为,这应该会让你接近
your code
*
*
*
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
link_title = html.body.getElementsByTagName("dl")(3).getElementsByTagName("a")(0).innerhtml
link_title = Replace(link_title, "<B>", "", 1, -1, vbTextCompare)
link_title = Replace(link_title, "</B>", "", 1, -1, vbTextCompare)
Debug.Print link_title
link_url = html.body.getElementsByTagName("dl")(3).getElementsByTagName("a")(0).getAttribute("href")
link_url = Replace(link_url, "about:", "http://torrentz.eu", 1, -1, vbTextCompare)
Debug.Print link_url
link_date = html.body.getElementsByTagName("dl")(3).getElementsByTagName("span")(1).innerhtml
link_date = Replace(link_date, "<SPAN title=""", "", 1, -1, vbTextCompare)
targ = InStr(1, link_date, """", vbTextCompare)
link_date = Left(link_date, -1 + targ)
Debug.Print link_date
continue your code