【发布时间】:2019-09-02 06:52:30
【问题描述】:
我正在尝试使用 vba 构建网络爬虫。我想将数据(街道地址、邮政编码和地区)从网站提取到工作表,但我遇到了问题。
<li id="ctl00_ctl00_cphMain_cphMainCol_CompanyDetailsInfoData1_liAddress" class="i-location" itemprop="address" itemscope="" itemtype="http://schema.org/Address">
<a href="javascript:void(0);" id="ctl00_ctl00_cphMain_cphMainCol_CompanyDetailsInfoData1_aShowOnMap" onclick="openMapTis(517648, 57522, 'KOVINARSTVO IVANETIČ d.o.o.|Omota 8 |Semič');">
<span itemprop="street-address">Omota 8</span>, <span itemprop="settlement">Omota</span>, <span itemprop="postal-code">8333</span> <span itemprop="locality">Semič</span>
</a>
</li>
Sub CompanyData()
Dim ie As InternetExplorer
Dim ht As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
'searching web address
ie.navigate ("https://www.bizi.si")
Do Until ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop
'searching company
Set ht = ie.document
ht.getElementsByTagName("Input").Item("ctl00$Search1$tbSearchWhat").Value = ThisWorkbook.Sheets("Podatki").Range("A1").Value
'click on search result
Set elems = ht.getElementsByTagName("a")
For Each elem In elems
If elem.className = "i-search" Then
elem.Click
Exit For
End If
Next
Application.Wait (Now + TimeValue("0:00:06"))
Set AllHyperLinks = ht.getElementsByTagName("a")
For Each hyper_link In AllHyperLinks
If hyper_link.innerText = Range("A1").Value Then
hyper_link.Click
Exit For
End If
Next
Application.Wait (Now + TimeValue("0:00:06"))
gf = ht.getElementsByTagName("span")(0).innerText
gf = Range("B2")
End Sub
我想从网站提取数据(街道地址、邮政编码和地区)到工作表。
【问题讨论】:
-
你应该更详细地解释你到底有什么问题。您的代码中哪里出现问题?给出的错误消息是什么(如果有)?您自己对问题进行了多少调查(例如,通过在代码中放入 debug.print 语句)?这些将对您的读者有很大帮助,并且意味着他们更有能力 - 并且愿意 - 帮助您..
-
问题开始于 gf = ht.getElementsByTagName("span")(0).innerText gf = Range("B2") 我想在 Excel 表上提取数据。这句话没问题。
-
有什么问题?你有错误吗?您是否打印出
gf以查看其中包含的内容?它是你所期望的吗?此外,如果您尝试将数据导出到 Excel,则应该是Range("B2").value = gf,而不是相反。 -
我按照你的建议进行了更改 (Range("B2").value = gf) - thanx 现在我得到了一些结果,但这个数据是错误的。 Omota 8 - 工作表上的结果应该是 Omota 8
标签: excel vba web-scraping