【发布时间】:2017-09-24 18:17:37
【问题描述】:
运行我的 vba 脚本,我可以看到它无法到达我想从中提取不同名称的目标页面。显然,我在代码中的某个地方搞砸了,但找不到。希望有人能帮助我找出我在哪里犯错误。谢谢。代码如下:
Sub Getmethod()
Dim http As New MSXML2.ServerXMLHTTP, html As New HTMLDocument
Dim topics, topic, ele As Object
Dim StrData As String
StrData = "what=Plumbers/where=All+States"
With http
.Open "GET", "http://www.yellowpages.co.za/search/" & StrData, False
.setRequestHeader "Content-Type", "text/xml"
.send
html.body.innerHTML = http.responseText
End With
Set topics = html.getElementsByClassName("resultName")
For Each topic In topics
Set ele = topic.getElementsByTagName("a")(0)
x = x + 1
Cells(x, 1) = ele.innerText
Next topic
End Sub
这里是表单元素:
<form action="/search/" class="searchForm" id="searchForm" autocomplete="off">
<div id="whatDiv">
<label class="searchLabel whatLabel" data-icon="">
<input type="search" id="whatField" name="what" class="searchField whatField" placeholder="Search for companies and services" autocomplete="off" tabindex="1">
</label>
</div>
<input type="submit" value="GO" class="searchBtn" tabindex="3" id="searchBtn">
<label class="searchLabel whereLabel" data-icon="">
<input type="search" id="whereField" name="where" class="searchField whereField" placeholder="Location" autocomplete="off" tabindex="2">
</label>
</form>
【问题讨论】:
-
您确定带有搜索参数的网址吗?如果我将此 URL 放在浏览器中,它不会返回任何内容。当我尝试在
cape town中搜索plumbers时,URL 看起来像这样:http://www.yellowpages.co.za/Search/plumbers/cape+town,+western+cape/1。所以也许你的网址不正确?您是否先手动尝试了此 URL? -
谢谢迪伊先生,您的回复。不,我没有手动尝试..
-
好吧,那么您可能应该先在浏览器中尝试一下,看看 URL 是否正确以及它是否确实返回了一些内容。
-
刚刚检查过它会转到我希望我的浏览器访问的那个页面。我手动尝试的网址是“yellowpages.co.za/search/Plumbers/All+States”
-
好的,那么
StrData可能不正确。应该只是Plumbers/All+States?
标签: json vba web-scraping xmlhttprequest