2017-10-17 112 views
-2

我正在嘗試使用VBA網頁刮取從Met辦公室網站爲倫敦拉最低夜間溫度。我試過使用代碼here。雖然代碼運行它不是複製我需要它複製。援助將不勝感激。使用vba從網站上刮取數據4

Sub WebScrape_1() 

'Create an Internet Explorer browser 
Dim appIE As Object 
Set appIE = CreateObject("internetexplorer.application") 

'Browse the browser to the target webpage 
With appIE 
    .Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07" 
    .Visible = True ' False activate when happly with code 
End With 

'Wait while loading 
Do While appIE.Busy 
    DoEvents 
Loop 

'What aspect of the webpage to copy 
Set allRowOfData = appIE.document.getElementById("nightValue0") 

Dim myValue As String: myValue = allRowOfData.innerHTML 

'Close the browser 
appIE.Quit 
Set appIE = Nothing 

'Paste the data into the selected range 
ActiveWorkbook.Sheets("Data_Temp").Range("C7").Value = myValue 

End Sub 
+3

'沒有luck'沒有描述任何種類的問題......我不認爲有人在這裏會浪費自己的時間猜測這是什麼意思 – jsotola

+1

同意@jsotola。你會得到一些結果嗎?錯誤的結果?沒有結果?錯誤消息?您需要提供有關運行代碼時發生的情況的更多信息。 – freginold

+0

稍微更新了該問題。不確定如何將我的代碼複製到網頁的相關部分。 – Karm

回答

2

容易問題Karm。對不起,我們在這個社區有一些粗魯的成員。

您只需在代碼中添加「FirstChild」和「innertext」即可。如果我是你,我會寫代碼,特別是在「等待加載」部分。這不是一個有效的方法。不管怎麼說,在下面找到你的工作代碼:

Sub WebScrape_1() 

'Create an Internet Explorer browser 
Dim appIE As Object 
Set appIE = CreateObject("internetexplorer.application") 

'Browse the browser to the target webpage 
With appIE 
    .Navigate "https://www.metoffice.gov.uk/public/weather/forecast/gcpvj0v07" 
    .Visible = True ' False activate when happly with code 
End With 

'Wait while loading 
Do While appIE.Busy 
    DoEvents 
Loop 

'What aspect of the webpage to copy 
Set allrowofdata = appIE.document.getElementById("nightValue0").FirstChild 
allrowofdata = allrowofdata.innertext 

'Close the browser 
appIE.Quit 
Set appIE = Nothing 

'Paste the data into the selected range 
ActiveWorkbook.Sheets("Sheet1").Range("C7").Value = myValue 

End Sub 
+1

這不是關於粗魯,而是關於詢問對其他人有用的明確問題,而不僅僅是OP - *這是本站的目的*:堆棧溢出不是「幫助** ME **調試**我的* *代碼「網站。不過你應該已經知道了 - 我確定你已經參加了[巡迴演唱會]並閱讀[問]了嗎?有1個代表和2個月的歷史,你可以保留這個元咆哮給自己。 –

+0

Thaks @rlemmi當我回到辦公室時,明天我會試試這個 – Karm

+0

不客氣!請不要忘記將您的標題改爲更有意義的內容。 @ Mat'sMug道歉,如果你把它冒犯了。我認爲對新成員禮貌是很好的。你想培養一個良好的社區。你和你的1,000+等級現在應該知道。你可以讓他以更好的方式改變標題或他的問題。 下次嘗試使用「請」或「謝謝」字樣。我希望這個用戶有1個代表和2個月的歷史能夠教你一些東西:)。 –