2017-01-29 6 views
3

나는 주가의 마지막 가격을 엑셀 워크 시트에 복사하려고합니다. Internet Explorer가 열리고 사이트로 이동하지만 마지막 가격이 내 워크 시트에 복사되지 않습니다. Debug.Print element.innertext를 추가하여 복사 된 항목이 있는지 확인하지만 직접 상자에는 아무 것도 표시되지 않습니다.Excel VBA ID로 항목 가져 오기

Dim element As IHTMLElement 
Dim elements As IHTMLElementCollection 

Dim ie As New InternetExplorer 
Dim html As HTMLDocument 

my_page = "http://www.morningstar.com/stocks/XASX/COH/quote.html" 

With ie 
    .Visible = TRUE 
    .Navigate my_page 

    Do While ie.readyState <> READYSTATE_COMPLETE 
    Loop 

    Set html = ie.document 
    Set element = html.getElementById("last-price-value")  
    Debug.Print element.innertext 

    Sheets("Sheet 1").Range("A2").Value = element.innertext  
End With 
+1

ID가 * last-price-value *로 설정된 요소는 코드에서 가리키는 페이지에 없습니다. 웹 브라우저에서 페이지를 열고 웹 도구를 열고 콘솔로 이동하여''document.getElementByID ('last-price-value')'''를 실행하면 내용을 확인할 수 있습니다. 문제는 코드가 아닌 선택기를 사용하는 것입니다. – mTorres

+0

감사합니다. 이 요소의 내부 텍스트를 복사하려고합니다 :

127.36
. 그렇다면 getElementsByCaseName()으로 코드를 변경해야합니까? 감사합니다 –

+0

귀하의 문제는 요소가 iframe 안에 있다는 것입니다. 직접 참조 할 수는 없습니다. 먼저 iframe 문서를 가져와야합니다. 그러면 iframe 문서를 가져와야합니다. 자, 어떻게해야할지 모르겠지만 그렇게 어렵지 않아야합니다, 당신은 [여기] 시작할 수 있습니다 (http://stackoverflow.com/questions/1088544/javascript-get-element-from-within- an-iframe). – mTorres

답변

0

그래서 문제가 iFrame- Iframe을 고통을 조금하고 특수 구문을 필요로 내 마지막 가격 값 요소가 포함되어 있고 :

내가 지금까지 무엇을 가지고 몇 가지 추가 단계로 데이터를 검색 할 수 있습니다 .- 기본적으로 iFrame을 찾고 .ContentDocument을 사용하여 HTML을 가져온 다음 평소처럼 HTML 문서를 사용할 수 있습니다. 그러나 iFrame에서 데이터를 추출하는 경우는 특히 그렇습니다 메인 웹 페이지가 iFrame이 다른 도메인에있는 하나의 도메인에 있기 때문에 고통 스럽습니다. 메인 페이지에서 iFrame의 HTML에 액세스 할 수 없게됩니다. 대신 할 일은 다음과 같습니다.(210) 1로드 메인 페이지 2 찾기 프레임 3 얻기 프레임의 URL 3-로드 프레임 URL 5 당겨 마지막으로 가격 값 ..

아래

참조 코드 예제 :

Public Sub sampleCode() 
Dim IE As New InternetExplorer 
Dim HTMLDoc As HTMLDocument 
Dim parentURL As String 
Dim frames As IHTMLElementCollection 
Dim frameURL As String 
Dim frameHTML As HTMLDocument 
Dim fCounter As Long 
Dim targetElement As HTMLObjectElement 

parentURL = "http://www.morningstar.com/stocks/XASX/COH/quote.html" 
'1- Load the main page 
With IE 
    .Visible = False 
    .Navigate parentURL 
    While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend 
    Set HTMLDoc = IE.document 
End With 

'2- Find the frame 
Set frames = HTMLDoc.getElementsByTagName("iframe") 
'Loop through all the frames 
For fCounter = 0 To frames.Length - 1 
    'Test each frame's ID to find the correct one (numerical appendix to ID can change so need to test for pre-fix) 
    If Left(frames(fCounter).ID, 10) = "QT_IFRAME_" Then 
     '3- Get the frame's URL 
     frameURL = frames(fCounter).src 
     Exit For 
    End If 
Next 

'4- Load the frame URL 
With IE 
    .Navigate frameURL 
    While .Busy Or .readyState <> READYSTATE_COMPLETE: Wend 
    Set frameHTML = IE.document 
End With 

'Find and pull the last price 
Set targetElement = frameHTML.getElementById("last-price-value") 
Sheets("Sheet 1").Range("A2").Value = CDbl(targetElement.innerText) 

IE.Quit 
Set IE = Nothing 
End Sub 

야후 소스로 전환하여 모호한 뮤추얼 펀드 또는 사설 펀드에 대한 가격을 인상하기 때문에 Morningstar가 필요하지 않으면 야후는 주식 가격을 끌어낼 수있는 매우 쉬운 VBA 인터페이스를 가지고 있습니다. .

호프가 도움이 되었으면 TheSilkCode