技术论坛

 抓取网页上的数据-这里以获取中央气象局气象数据为例

返回主题列表
作者 主题
柳叶飘飘
侠圣

经验值:2495
发帖数:95
精华帖:7
楼主    2018-12-20 16:51:52
主题:抓取网页上的数据-这里以获取中央气象局气象数据为例

   现在网络越来越发达了,对组态软件要求也越来越多,我参考了网上的一些例子,但是获取的数据都不是很多,对于搞暖通楼控的工程师来说温度、湿度、和大气压力都很重要,若你要获得更多的气象数据网上提供的API是要收费的。我通过分析网站的数据,得到了更详细的数据,免费。这里我自己写了一个抓取天气信息的VBS脚本,有兴趣可以适当的改改弄到组态软件当中去,这个是网页抓取JSON格式的,这个可以作为参考如何从网页当中抓取数据。 不同的组态软件有不同的办法,这个VSB脚本提供给大家参考。 我这个抓取的原始数据信息相当较全面,由于VBS不是JAVA不能直接解析JSON,我这里用了正则表达式提取了几个数据作为弹窗。不同的组态软件都可以参考这个进行获取数据达到组态软件中。。。。本例子获得的气象数据为成都市双流区的。。你懂得我家在这里 改动参数就可以获得别的地方的气象数据。这个是中央气象台的后台实时数据。。


   题外话:最近也比较忙,其实挺想网上看些资料写个关于不同经纬度不同日期时间的日照,太阳高度等西门子SCL的程序。。PLC就可以计算了对做楼控的兄弟就很有用了。。有兴趣的支持我一下。


Dim html,http,strHtml,post_list,window,kuwo,tet,fso,f,t

 Dim regEx, Match, Matches

 DIM StrTEST

'Set html = CreateObject("htmlfile") 

Set http = CreateObject("Msxml2.ServerXMLHTTP") 

'html.designMode = "on" ' 开启编辑模式 

http.open "GET", "http://www.nmc.cn//f/rest/real/56288", False 

http.send 

strHtml = trim(http.responseText)

'msgbox strHtml


'html.write strHtml ' 写入数据 

'Set post_list = html.getElementById("post_list") 

'For Each el In post_list.children     Ws cript.Echo el.getElementsByTagName("a")(0).innerText 

'Next



'#################################################


'strHtml = http.responseText ' 得到数据 

'Set window = html.parentWindow 

'window.execs cript strHtml, "Js cript"     '定义一个JS变量,将JSON数据引入

'Set kuwo = window.returnCitySN ' 获取解析后的对象

   '  MsgBox"温度 " & window & " 摄氏度"

'Set kuwo = kuwo.json ' 获取解析后的对象 


'Ws cript.Echo "温度:", kuwo.temperature, " 湿度:", kuwo.humidity


'MsgBox window.Eval("json.temperature")

'#################################################

'

   ' MSGBOX sc.Eval("o.temperature")


   

 set fso=createobject("s cripting.filesystemobject")

 

set f=fso.opentextfile("c:a.txt",2,true):f.write strHtml :f.close

set f=fso.opentextfile("c:a.txt",1)

strHtml = f.ReadAll

MsgBox strHtml


 ' 创建变量。

 StrTEST="temperature"

 

  Set regEx = New RegExp         ' 创建正则表达式。

  regEx.Pattern = StrTEST          ' 设置模式。

  regEx.IgnoreCase = True         ' 设置为不区分大小写。

  regEx.Global = True         ' 设置全局适用。

  Set Matches = regEx.Execute(strHtml)   ' 执行搜索。

For Each Match in Matches      ' 对 Matches 集合进行迭代。

RetStr = RetStr & Match.FirstIndex 

Next


MSGBOX "温度为"&Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,Instr(1, Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,6),",", 1)-1)

StrTEST="humidity"

 RetStr=""

 

  regEx.Pattern = StrTEST          ' 设置模式。

  regEx.IgnoreCase = True         ' 设置为不区分大小写。

  regEx.Global = True         ' 设置全局适用。

  Set Matches = regEx.Execute(strHtml)   ' 执行搜索。

For Each Match in Matches      ' 对 Matches 集合进行迭代。

    'RetStr = RetStr & "Match found at position "

    'RetStr = RetStr & Match.FirstIndex & ". Match Value is '"

    'RetStr = RetStr & Match.Value & "'." & vbCRLF

RetStr = RetStr & Match.FirstIndex 

'RetStr =  RetStr & Match.Value & ":" & vbCRLF

Next

'MSGBOX RetStr


MSGBOX "湿度为"&Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,Instr(1, Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,6),",", 1)-1)



StrTEST="airpressure"

 RetStr=""

 

  'Set regEx = New RegExp         ' 创建正则表达式。

  regEx.Pattern = StrTEST          ' 设置模式。

  regEx.IgnoreCase = True         ' 设置为不区分大小写。

  regEx.Global = True         ' 设置全局适用。

  Set Matches = regEx.Execute(strHtml)   ' 执行搜索。

For Each Match in Matches      ' 对 Matches 集合进行迭代。


RetStr = RetStr & Match.FirstIndex 

Next


MSGBOX "大气压为"&Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,Instr(1, Mid(strHtml, LEFT(RetStr,3)+LEN(StrTEST)+3,6),",", 1)-1)

'set html=nothing

set http=nothing

天气数据抓取.rar


努力赚钱 养家糊口
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。