Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA代码从网页中提取数据_Vba - Fatal编程技术网

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

使用VBA代码从网页中提取数据,vba,Vba,我正在编写一个visual basic代码来自动提取一些天气数据。以下是该网页的源代码 <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%"

我正在编写一个visual basic代码来自动提取一些天气数据。以下是该网页的源代码

<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>

例子:
纬度33.5
经度-80.75 或 纬度33 30
经度-80 45 纬度? 南部:-90比0 北:0至90 经度? 西:180比0 东:0至180 如果输入超出范围,则此表单为“重置”。
我收到一个错误(对象变量或块变量未设置)。有人能帮我查一下这个密码吗?事先非常感谢。以下是我写的:

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
Sub-extractSolData()
模糊的物体
将纬度设置为字符串,将经度设置为字符串
设置IE=CreateObject(“InternetExplorer.Application”)
纬度=输入框(“输入位置的纬度”)
经度=输入框(“输入位置的经度”)
与IE
.Visible=True
.导航(“https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?email=skip@larc.nasa.gov.)
而IE.readyState 4
多芬特
温德
IE.document.getElementsByName(“lat”).Item(0)。值=纬度
IE.document.getElementsByName(“lon”).Item.innertext=经度
IE.document.getElementsByName(“提交”).Item.Value=“提交”
忙的时候做,忙的时候做_
.readyState 4
多芬特
环
以
设置IE=无
端接头

问题在于您在中使用的引号

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 

通过将long上的lat连接到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
选项显式
公署助理署长()
将纬度设置为字符串,将经度设置为字符串
纬度=输入框(“输入位置的纬度”)
经度=输入框(“输入位置的经度”)
如果纬度=vbNullString或经度=vbNullString,则退出Sub
使用CreateObject(“internetexplorer.application”)
.Visible=True
.导航“https://eosweb.larc.nasa.gov/cgi-bin/sse/grid.cgi?email=skip%40larc.nasa.gov&step=1&lat=&LATITUDE&&lon=“&LATITUDE&&submit=submit”
当.Busy或.readyState<4:DoEvents:Wend时
“使用新页面做一些事情
“退出”<稍后取消对此的注释
以
端接头

您需要准确指出错误发生的位置
getElementsByName
始终返回一个集合(即使文档中只有一个匹配项),因此无论何时使用它,都需要处理该集合中的单个元素。感谢Tim提供的提示!!嗨,珍诺!谢谢你抓我的兄弟!!成功了。坦白地说,我不记得从任何网站复制过它们,但我使用了记事本文件中的早期代码。也许这些是从那里来的。再次非常感谢。