2017-10-17 116 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个月的历史能够教你一些东西:)。 –