使用VBA代码从网页中提取数据

时间:2015-04-16 15:17:30

标签: vba

我正在制作视觉基本代码,以自动提取一些天气数据。以下是网页的源代码。

<tr><td>
<table align="center" width="100%" summary="table used for formatting"><tr>
<td align="center"><b>Example:</b></td>
<td align="center" width="40%">Latitude 33.5<br>Longitude -80.75</td>
<td align="center">OR</td>
<td align="center" width="40%">Latitude 33 30<br>Longitude -80 45</td>
</tr></table></td></tr>
<tr><td><table width="100%" summary="table used for formatting"><tr>
<td><b><label for="lat">Latitude? </label>
<input type="text" name="lat" id="lat" size="12" value=""></b></td>
<td width="30%">South:&nbsp;&nbsp; -90 to 0</td>
<td width="30%">North:&nbsp;&nbsp; 0 to 90</td>
</tr><tr>
<td><b><label for="lon">Longitude? </label>
<input type="text" name="lon" id="lon" size="12" value=""></b></td>
<td width="30%">West:&nbsp;&nbsp; -180 to 0</td>
<td width="30%">East:&nbsp;&nbsp; 0 to 180</td>
</tr></table></td></tr>
<tr><td><table align="center" summary="table used for formatting"><tr>
<td><b> <input type="submit" name="submit" value="Submit"> </b></td>
<td><b> <input type="submit" name="submit" value=" Reset "> </b></td>
<td><i> This form is "Reset" if the input is out of range. </i></td>
</tr></table>
</td></tr></table>
</form>

我收到错误(对象变量或未设置块变量)。有人可以帮我解决这个问题吗?非常感谢你提前。这是我写的:

Sub extractSolData()
Dim IE As Object
Dim latitude As String, longitude As String


Set IE = CreateObject("InternetExplorer.Application")

latitude = InputBox("Enter Latitude of the location")
longitude = InputBox("Enter Longitude of the location")

With IE
.Visible = True
.navigate ("https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?email=skip@larc.nasa.gov")

While IE.readyState <> 4
DoEvents
Wend

IE.document.getElementsByName(“lat”).Item(0).Value = latitude
IE.document.getElementsByName(“lon”).Item.innertext = longitude


IE.document.getElementsByName("submit").Item.Value = "Submit"

Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
End With

Set IE = Nothing

End Sub

2 个答案:

答案 0 :(得分:1)

问题在于您在

中使用的引号
IE.document.getElementsByName(“lat”).Item(0).Value = latitude
IE.document.getElementsByName(“lon”).Item.innertext = longitude

这些不是真正的引用,我敢打赌你从网站上复制了这个,不知怎的,引号被搞砸了。他们需要看起来像

IE.document.getElementsByName("lat").Item(0).Value = latitude
IE.document.getElementsByName("lon").Item.innertext = longitude 

答案 1 :(得分:0)

你可以通过将lat on long连接到URL字符串来缩短以下所有内容:

Option Explicit
Public Sub nota_ong()
    Dim latitude As String, longitude As String

    latitude = InputBox("Enter Latitude of the location")
    longitude = InputBox("Enter Longitude of the location")

    If latitude = vbNullString Or longitude = vbNullString Then Exit Sub

    With CreateObject("internetexplorer.application")
        .Visible = True
        .navigate "https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?email=skip%40larc.nasa.gov&step=1&lat=" & LATITUDE & "&lon=" & LONGITUDE & "&submit=Submit"
        While .Busy Or .readyState < 4: DoEvents: Wend
        'Do stuff with new page
        'Quit '< Uncomment this later
    End With
End Sub