如何获取第一个url下方的描述/文字,以便在Googlesearch结果中显示?
如何得到第一个链接的描述出现在谷歌search结果?
testing代码更新1/24/2017
得到'对象不支持方法/属性'消息
Dim URl As String, lastRow As Long Dim xmlHttp As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object lastRow = Range("A" & Rows.Count).End(xlUp).Row For i = 2 To lastRow URl = "https://www.google.co.in/search?q=" & Cells(i, 1) Set xmlHttp = CreateObject("MSXML2.XMLHTTP") xmlHttp.Open "GET", URl, False xmlHttp.setRequestHeader "Content-Type", "text/xml" xmlHttp.send Set html = CreateObject("htmlfile") html.body.innerHTML = xmlHttp.ResponseText Set objResultDiv = html.getelementbyid("rso") Set objH3 = objResultDiv.getelementsbytagname("H3")(0) Set link = objH3.getelementsbytagname("a")(0) Set objResultDiv = html.GetElementsByClassName("f slp")(0) str_text = Replace(link.innerHTML, "<EM>", "") str_text = Replace(str_text, "</EM>", "") Cells(i, 2) = str_text Cells(i, 3) = link.href Cells(i, 4) = objResultDiv.innerText
Span class =“st”
我相信你正在寻找<div class="rc">
里面的<div class="rc">
<span class="st">
<div class="rc">
编辑#1
如果我们search投资,股票,期货和期权会计 ,我们希望文本R. Venkata Subramani – 2011 – 商业与经济 ,在绿色的url下面,如图:
那么我们需要的确是<div class="f slp">
OP <div class="f slp">
,正如你指出的那样。
所以要从中获取文本,我们可以使用代码:
set objResultDiv = html.GetElementsByClassName("f slp")(0) 'note the plural, element*s* cells(i, 4) = objResultDiv.innerText
对象不支持此属性或方法响应
HTMLDocument有一个额外的方法来获取元素的集合。
set objResultDiv = .querySelectorAll(".f.slp") cells(i, 4) = objResultDiv(index).innerText` 'replace index with number