【发布时间】:2020-09-18 18:44:56
【问题描述】:
我正在尝试使用 VBA 网络抓取从气象局网站获取伦敦的最低夜间温度。我尝试使用here 发布的代码。当代码运行时,它不会复制我需要它复制的内容。非常感谢您的帮助。
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
【问题讨论】:
-
no luck没有描述任何类型的问题......我认为这里的任何人都不会浪费时间猜测这意味着什么 -
同意@jsotola。你得到一些结果吗?错误的结果?没有结果?错误信息?您需要提供有关运行代码时发生的情况的更多信息。
-
稍微更新了问题。不确定如何让我的代码复制网页的相关部分。
-
我已经快速检查了您的代码。对我来说,它适用于 IE 部分,所以我最终以
myValue持有<span>元素和夜晚的温度。由于这仍然是 HTML 代码,因此您需要一些字符串处理来提取温度。在Quit-statement 上设置断点,检查myValue的内容是什么。 -
哦 (facepalm) - 或许考虑使用实际描述问题所在的标题?当人们遇到特定的编码问题时,他们会通过搜索引擎访问该网站:拥有一个可搜索的标题,或多或少是您在谷歌上寻找解决方案的内容,这对这个网站来说是一个更好的问题。