|
onlycxb 发表于 2014-11-7 10:49
多谢onlycxb大侠分享!
分别用网抓及自定义函数获得城市拼音码,并取数如下:
Option Explicit
Sub 天气3()
Dim W As New WinHttp.WinHttpRequest, X As New MSXML2.XMLHTTP
Dim js As New MSScriptControl.ScriptControl, arr
Dim StrText As String, HTML As Object, tb, i%
Dim MyCity, eMyCity As String
Dim 更新日期 As String, 白天温度 As String, 夜间温度 As String, 天气 As String
MyCity = InputBox("请输入城市名", "城市", "北京")
Set HTML = CreateObject("htmlfile")
HTML.DesignMode = "on"
With W
.Open "GET", "http://open.weather.sina.com.cn/api/location/getIndexSuggestion/" & MyCity, False
.setRequestHeader "Referer", "http://weather.sina.com.cn/"
.send
eMyCity = Split(Split(.responseText, """url"":""")(1), """")(0)
End With
With X
.Open "GET", "http://weather.sina.com.cn/" & eMyCity, False
.send
StrText = Replace(.responseText, vbCrLf, "")
End With
HTML.Write StrText
Set tb = HTML.all.tags("p")
For i = 0 To tb.Length - 1
If tb(i).classname = "wt_fc_c0_i_day wt_fc_c0_i_today" Then
更新日期 = tb(i - 1).innertext
白天温度 = Split(tb(i + 3).innertext, "/")(0)
夜间温度 = Split(tb(i + 3).innertext, "/")(1)
天气 = Split(tb(i + 2).innertext, " ")(0) & ":" & tb(i + 1).ChildNodes(0).alt _
& "," & Split(tb(i + 2).innertext, " ")(1) & ":" & tb(i + 1).ChildNodes(2).alt _
& "; " & tb(i + 4).innertext
End If
Next i
MsgBox MyCity & 更新日期 & "天气情况:" & vbCrLf & "白天温度:" & 白天温度 & vbNewLine & "夜间温度:" & 夜间温度 & Chr(10) & "天气:" & 天气, , "城市天气预报"
End Sub
Sub 天气4()
Dim X As New MSXML2.XMLHTTP
Dim js As New MSScriptControl.ScriptControl, arr
Dim StrText As String, HTML As Object, tb, i%
Dim MyCity As String
Dim 更新日期 As String, 白天温度 As String, 夜间温度 As String, 天气 As String
MyCity = InputBox("请输入城市名", "城市", "北京")
Set HTML = CreateObject("htmlfile")
HTML.DesignMode = "on"
With X
.Open "GET", "http://weather.sina.com.cn/" & PinYin(MyCity, "", 2), False
.send
StrText = Replace(.responseText, vbCrLf, "")
End With
HTML.Write StrText
Set tb = HTML.all.tags("p")
For i = 0 To tb.Length - 1
If tb(i).classname = "wt_fc_c0_i_day wt_fc_c0_i_today" Then
更新日期 = tb(i - 1).innertext
白天温度 = Split(tb(i + 3).innertext, "/")(0)
夜间温度 = Split(tb(i + 3).innertext, "/")(1)
天气 = Split(tb(i + 2).innertext, " ")(0) & ":" & tb(i + 1).ChildNodes(0).alt _
& "," & Split(tb(i + 2).innertext, " ")(1) & ":" & tb(i + 1).ChildNodes(2).alt _
& "; " & tb(i + 4).innertext
End If
Next i
MsgBox MyCity & 更新日期 & "天气情况:" & vbCrLf & "白天温度:" & 白天温度 & vbNewLine & "夜间温度:" & 夜间温度 & Chr(10) & "天气:" & 天气, , "城市天气预报"
End Sub
|
评分
-
1
查看全部评分
-
|