Excel 从网页上的span itemprop提取数据
我试图建立一个网页刮板使用vba。我想提取数据(街道地址,邮政编码和地点)从网站到工作表,但我有问题Excel 从网页上的span itemprop提取数据,excel,vba,web-scraping,Excel,Vba,Web Scraping,我试图建立一个网页刮板使用vba。我想提取数据(街道地址,邮政编码和地点)从网站到工作表,但我有问题 <li id="ctl00_ctl00_cphMain_cphMainCol_CompanyDetailsInfoData1_liAddress" class="i-location" itemprop="address" itemscope="" itemtype="http://schema.org/Address"> <a
<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
子公司数据()
Dim ie作为InternetExplorer
作为HTMLDocument的Dim ht
Set ie=新的InternetExplorer
可见=真实
'搜索网址
例如,导航(“https://www.bizi.si")
直到ie.readyState=readyState\u完成为止
多芬特
环
“搜索公司
Set ht=ie.document
ht.getElementsByTagName(“输入”).Item(“ctl00$Search1$tbSearchWhat”).Value=ThisWorkbook.Sheets(“Podatki”).Range(“A1”).Value
'单击搜索结果
Set elems=ht.getElementsByTagName(“a”)
对于元素中的每个元素
如果elem.className=“i-search”,则
元素。点击
退出
如果结束
下一个
Application.Wait(现在+时间值(“0:00:06”))
设置AllHyperLinks=ht.getElementsByTagName(“a”)
对于所有超链接中的每个超链接
如果hyper_link.innerText=范围(“A1”)。值,则
超链接。点击
退出
如果结束
下一个
Application.Wait(现在+时间值(“0:00:06”))
gf=ht.getElementsByTagName(“span”)(0).innerText
gf=范围(“B2”)
端接头
我想将数据(街道地址、邮政编码和地点)从网站提取到工作表。该页面实际上使用公司名称构建了一个查询字符串url;因此,您只需要将公司名称添加到基本url的末尾(而不是在页面上输入)。您也可以只使用xhr而不是慢速浏览器(url编码公司名称) 我用于匹配地址的相应表元素。css选择器是通过以下方法应用的 正则表达式只是进行一些字符串整理以删除多余的空白
Internet Explorer:
Option Explicit
Public Sub CompanyData()
Dim ws As Worksheet, re As Object
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\s{2,}"
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate2 "https://www.bizi.si/iskanje?q=" & ws.Range("A1").Value
While .Busy Or .readyState <> 4: DoEvents: Wend
ws.Range("B1").Value = re.Replace(Join$(Array(.document.querySelector("td.item a").innerText, .document.querySelector("td.item + td.item").innerText), ", "), Chr$(32))
.Quit
End With
End Sub
参考资料(VBE>工具>参考资料):
Option Explicit
Public Sub CompanyData()
Dim ws As Worksheet, re As Object
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\s{2,}"
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate2 "https://www.bizi.si/iskanje?q=" & ws.Range("A1").Value
While .Busy Or .readyState <> 4: DoEvents: Wend
ws.Range("B1").Value = re.Replace(Join$(Array(.document.querySelector("td.item a").innerText, .document.querySelector("td.item + td.item").innerText), ", "), Chr$(32))
.Quit
End With
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的结果