如何实时获取网页上的数据信息!!

shen_wei 2010-11-25 04:18:58
现在做一个dlg的窗口,实时获取某一网站上的天气信息。。
这个应该怎么做?本人现在不知道怎么做,请大家给些思路或者方法。。。

例如:获取www.weather.com.cn/上的天气信息!!显示到界面上。。。
...全文
3523 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyxie 2010-11-26
  • 打赏
  • 举报
回复
如果你自己去解析,需要2步:
1) 解析出信息
2) 自己在程序中把这些信息展示出来(显示文字,画图等)

还是建议用webbrowser控件,通过 IHTMLDocument2, IHTMElement等接口进行操作,让webbrowser帮你实现显示的问题。这样比较简单~

vc dlg中用webbrowser控件的教程一大堆,你自己搜一下哈~
采用这种方式主要就是通过 IHTMLDocument2, IHTMElement等接口进行操作,使webbrowser控件只显示你感兴趣的内容,把不需要显示的html元素去掉,或者直接对body元素put_innerHtml 为你感兴趣的那部分的源代码!
shen_wei 2010-11-26
  • 打赏
  • 举报
回复

<!--day 1-->
<table class="yuBaoTable" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" rowspan="2" class="t0" style="background:#f4f7fc;">26日星期五</td>
<td width="12%" style="background:#f4f7fc;">白天</td>
<td width="10%">
<a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">
<img src="/m2/i/icon_weather/29x20/d00.gif" />
</a>
</td>
<td width="18%"><a href ="http://baike.weather.com.cn/index.php?doc-view-767.php" target="_blank">晴</a></td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-1148.php" target="_blank">
<b>高温 <strong>23<strong>℃</b>
</a>
</td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
无持续风向
</a>
</td>
<td width="15%">
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
微风
</a>
</td>
</tr>
<tr>
<td style="background:#f4f7fc;">夜间</td>
<td>
<a href="http://www.weather.com.cn/static/html/legend.shtml" target="_blank">
<img src="/m2/i/icon_weather/29x20/n00.gif" />
</a>
</td>
<td><a href ="http://baike.weather.com.cn/index.php?doc-view-767.php" target="_blank">晴</a></td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-1386.php" target="_blank">
<span>低温 <strong>16<strong>℃</span>
</a>
</td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
无持续风向
</a>
</td>
<td>
<a href="http://baike.weather.com.cn/index.php?doc-view-778.php" target="_blank">
微风
</a>
</td>
</tr>
</table>


对于这个样的html。。我不知道怎么解析才能获取到里面的关键字。。请大家给详细说明下。。谢谢!!
shen_wei 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 qiri07 的回复:]
给你个例子


C/C++ code

BOOL bNext1=FALSE,bNext2=FALSE,bNext3=FALSE;
int leftpos=0; //取某行源码“</TD>”的位置
int rightpos=0; //取某行源码“>”的位置
int isel=m_reportlist.GetSelectionMark();
……
[/Quote]

楼上的谢谢你了。这个例子我看过了。。。
qiri07 2010-11-26
  • 打赏
  • 举报
回复
给你个例子


BOOL bNext1=FALSE,bNext2=FALSE,bNext3=FALSE;
int leftpos=0; //取某行源码“</TD>”的位置
int rightpos=0; //取某行源码“>”的位置
int isel=m_reportlist.GetSelectionMark();
if(isel<0)
{
AfxMessageBox("请选择城市");
return;
}
address.Format("%s/%s.html",addressfront,city2[isel][1]);
strtmp1.Format("city\" >%s",city2[isel][0]);
CString strsource;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
myHttpFile=(CHttpFile*)mySession.OpenURL(address);
while(myHttpFile->ReadString(strsource))
{
//如果某行源码中有提取的字符,开始进行处理
if(strsource.Find(strtmp1)>0)
bNext1=TRUE;
if(bNext1)
{
int leftpos=strsource.Find("map-layer-weaher");

if(leftpos>0)
{
strtmp2=strsource.Right(strsource.GetLength()-strlen("map-layer-weaher")-leftpos-2);
rightpos=strtmp2.Find("</div>");
strtmp2=strtmp2.Left(rightpos);
strweather=strtmp2;
bNext1=FALSE;
bNext2=TRUE;
}
}
if(bNext2)
{
int leftpos=strsource.Find("map-layer-temp");
if(leftpos>0)
{
strtmp2=strsource.Right(strsource.GetLength()-strlen("map-layer-temp")-leftpos-2);
rightpos=strtmp2.Find("</div>");
strtmp2=strtmp2.Left(rightpos);
strtemperature=strtmp2;
bNext2=FALSE;
bNext3=TRUE;
}
}
if(bNext3)
{
int leftpos=strsource.Find("map-layer-wind");
if(leftpos>0)
{
strtmp2=strsource.Right(strsource.GetLength()-strlen("map-layer-wind")-leftpos-2);
rightpos=strtmp2.Find("</div>");
strtmp2=strtmp2.Left(rightpos);
strwind=strtmp2;
bNext3=FALSE;
goto end;
}
}

}
end:
myHttpFile->Close();
mySession.Close();
m_reportlist.SetItemText(isel,1,strweather);
m_reportlist.SetItemText(isel,2,strtemperature);
m_reportlist.SetItemText(isel,3,strwind);
shen_wei 2010-11-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 skyxie 的回复:]
最合适的就是在你的对话框中嵌入webbrowser控件,然后通过webbrowser控件显示这个页面。通过MSHTML显示页面,把不需要的网页部分隐藏起来就可以了!
[/Quote]
MSHTML 怎么控制页面的显示??也就是只显示一部分出来?麻烦你给解析下了!谢谢了
  • 打赏
  • 举报
回复
楼上的建议非常好!
skyxie 2010-11-25
  • 打赏
  • 举报
回复
最合适的就是在你的对话框中嵌入webbrowser控件,然后通过webbrowser控件显示这个页面。通过MSHTML显示页面,把不需要的网页部分隐藏起来就可以了!
Eleven 2010-11-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 shen_wei 的回复:]
获得到的网页源码。。然后根据网页源码自己在分析。。。

是这样的吗?网页代码怎么分析,才能找到天气的那一部分。。。

例如这个网页。。http://www.weather.com.cn/weather/101010100.shtml

需要做哪些分析。。。
[/Quote]
说白了就是解析字符串,从中筛选出自己想要的信息
shen_wei 2010-11-25
  • 打赏
  • 举报
回复
获得到的网页源码。。然后根据网页源码自己在分析。。。

是这样的吗?网页代码怎么分析,才能找到天气的那一部分。。。

例如这个网页。。http://www.weather.com.cn/weather/101010100.shtml

需要做哪些分析。。。
Eleven 2010-11-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 shen_wei 的回复:]
引用 5 楼 visualeleven 的回复:
又是做网页爬虫


给指点下应该怎么做这个。。谢谢
[/Quote]
就是得到指定的网页内容,然后自己解析得到想要的数据
可以使用CInternetSession类的相关函数OpenURL();
shen_wei 2010-11-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 visualeleven 的回复:]
又是做网页爬虫
[/Quote]

给指点下应该怎么做这个。。谢谢
Eleven 2010-11-25
  • 打赏
  • 举报
回复
又是做网页爬虫
shen_wei 2010-11-25
  • 打赏
  • 举报
回复
楼上的能说详细点吗?WebService 这个怎么用?、
majy 2010-11-25
  • 打赏
  • 举报
回复
上面的方法比较合适,人家提供接口

如果一定要从普通的网站拉数据,那只有自己写http请求,把网页的内容拉下来,分析网页数据,把想要的数据提取出来
ToperRay 2010-11-25
  • 打赏
  • 举报
回复
WebService, OnTimer
yuchongjike 2010-11-25
  • 打赏
  • 举报
回复
你干嘛不直接调WebService,有一个网站有提供这个接口的.好像是webXML吧

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧