从网站源代码中提取特定的不同元素

我试图从网站提取特定的链接,我无法拉入string。

我必须从一个网站search大约5000家公司,所有的链接都有所不同。 一个示例公司(诺基亚)的源代码的链接是这样的:view-source: http : //finder.fi/yrityshaku/Nokia+oyj这是我正在看的部分:

<div class="itemName"> <!-- Yritysnimi --> <!-- Aukeaa aina yhteystiedot-vÃ?lilehdelle --> <a href="/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159838" class="resultGray"> 

我想提取之间的子串

  <!-- Yritysnimi --> <!-- Aukeaa aina yhteystiedot-vÃ?lilehdelle --> <a href=" 

 " class="resultGray"> 

这个子string会随着我search的每个公司而有所不同,所以我只会知道string在我试图提取的子string周围。

我试过使用browserIE.Document.body.innerHTML

 Sub Macro1() Set browserIE = CreateObject("InternetExplorer.Application") browserIE.Top = 0 browserIE.Left = 800 browserIE.Width = 800 browserIE.Height = 1200 browserIE.Visible = True Set ws = ThisWorkbook.Worksheets("Sheet1") browserIE.Navigate ("http://www.finder.fi/yrityshaku") Do DoEvents Loop Until browserIE.ReadyState = 4 browserIE.Document.getElementById("companysearchform_query_companySearchTypename").Click browserIE.Document.getElementById("SearchInput").Value = "nokia oyj" browserIE.Document.getElementById("SearchSubmit").Click Application.Wait (Now + TimeValue("0:00:4")) codeArea = Mid(V, InStr(V, "<div class=""itemName""> <!-- Yritysnimi --> <!-- Aukeaa aina yhteystiedot-vÃ?lilehdelle --> <a href="""), Len(V)) Debug.Print codeArea theLink = Mid(codeArea, 117, InStr(codeArea, """ class=""resultGray"">" - 1)) End Sub 

但我得到一个无效的过程调用或参数

我研究了一些,但我还没有find合适的解决scheme。 有些人build议只从源代码中提取一个元素,其他人则将整个源代码复制到一个stringvariables中。 作为一个不太熟练的人,我宁愿把整个代码放到一个string中,因为我认为这样会更容易理解。

原始网站(芬兰语) http://finder.fi/yrityshaku/nokia+oyj

您需要find所有具有itemName类名的<div>元素。 通过循环来find<a>元素,并使用第一个获取href属性。

 Sub Macro1() Dim browserIE As Object, ws As Worksheet Set browserIE = CreateObject("InternetExplorer.Application") browserIE.Top = 0 browserIE.Left = 800 browserIE.Width = 800 browserIE.Height = 1200 browserIE.Visible = True Set ws = ThisWorkbook.Worksheets("Sheet1") browserIE.Navigate ("http://www.finder.fi/yrityshaku") Do While browserIE.ReadyState <> 4 And browserIE.Busy: DoEvents: Loop browserIE.Document.getElementById("companysearchform_query_companySearchTypename").Click browserIE.Document.getElementById("SearchInput").Value = "nokia oyj" browserIE.Document.getElementById("SearchSubmit").Click Do While browserIE.ReadyState <> 4 And browserIE.Busy: DoEvents: Loop 'Application.Wait (Now + TimeValue("0:00:4")) Dim iDIV As Long With browserIE.Document.body If CBool(.getelementsbyclassname("itemName").Length) Then 'there is at least one div with the itemName class For iDIV = 0 To .getelementsbyclassname("itemName").Length - 1 With .getelementsbyclassname("itemName")(iDIV) If CBool(.getelementsbytagname("a").Length) Then 'there is at least one anchor element inside this div Debug.Print .getelementsbytagname("a")(0).href End If End With Next iDIV End If End With End Sub 

我通过VBE的工具►参考添加了Microsoft HTML对象库Microsoft Internet控件

立即窗口的结果。

 http://www.finder.fi/Televiestint%C3%A4laitteita+ja+palveluja/Nokia+Oyj/ESPOO/yhteystiedot/159843 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia/SALO/yhteystiedot/960395 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia/TAMPERE/yhteystiedot/853264 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia/ESPOO/yhteystiedot/2931747 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia/ESPOO/yhteystiedot/2931748 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia/TAMPERE/yhteystiedot/835172 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159838 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/SALO/yhteystiedot/159839 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159850 http://www.finder.fi/Tietoliikennepalveluja%2C+tietoliikennelaitteita/Nokia+Oyj/TAMPERE/yhteystiedot/159857