2016-01-04 68 views
3

我試圖從一個網站的特定鏈接,我有麻煩揪成字符串一個特定的變動成分。提取來自網站源代碼

我從網站搜索約5000個企業和所有環節的不同而不同。一個實例公司(諾基亞)的源代碼的鏈接是:查看源代碼: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"> 

此子將與每家公司我搜索有所不同,所以我只知道字符串是圍繞着子,我試圖提取。

我試着使用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 

,但我得到一個無效的過程調用或參數

我研究過一些,但我還沒有找到一個合適的解決方案呢。有些人建議只從源代碼中提取一個元素,其他人則將整個源代碼複製到一個字符串變量中。作爲一個在vba方面不太熟練的人,我寧願將整個代碼放到一個字符串中,因爲我認爲這樣會更容易理解。

原網站(芬蘭)http://finder.fi/yrityshaku/nokia+oyj

+2

嘗試使用一個循環來捕獲就緒狀態和忙狀態,以便不忙和readystate完成。這會讓你擺脫4秒的等待。什麼是V? –

+0

我編輯的代碼,所以我不知何故離開了V = browserIE.Document.body – Joonas

回答

2

你需要找到所有<div>元素與類名ITEMNAME。循環通過這些找到<a>元素(S)和使用第一個拿到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 

我加Microsoft HTML對象庫Microsoft Internet控制經由VBE的工具項目►參考

結果從即時窗口。

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 
+0

我運行Microsoft HTML對象庫和添加,但它並沒有在即時窗口中產生任何東西,或其他任何地方爲此事Microsoft Internet控制您的代碼。任何想法爲什麼? – Joonas

+0

我試着cheking什麼browserIE.Document.body持有,但調試只能說明:對象HTMLBodyElement。 – Joonas

+0

得到它的工作,實施了Application.Wait(Now + TimeValue(「0:00:4」))。奇蹟般有效! – Joonas