⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsehtml.cpp

📁 利用Boost库的正则表达式从中国气象网取天气预报,并将其写入Oracle数据库
💻 CPP
字号:
#include "StdAfx.h"
#include "ParseHTML.h"
#include <boost/regex.hpp>
#include <string>

CParseHTML::CParseHTML(void)
{
}

CParseHTML::~CParseHTML(void)
{
}


// 获取HTML中的一个标志段落
BOOL CParseHTML::GetHtmlSignSection(CString html,CString begin_str,CString end_str,CString& rets,int begin_count,int end_count)
{
	int begin,ends,temp=0;
	for (int b=0;b<begin_count;b++)
	{
		begin=html.Find(begin_str,temp);
		if (begin==-1)
			return FALSE;
		temp=begin+begin_str.GetLength();
	}
	temp=begin;
	for (int i=0;i<end_count;i++)
	{
		ends=html.Find(end_str,temp);
		if (ends==-1)
			return FALSE;
		temp=ends+end_str.GetLength();
	}
	rets=html.Mid(begin,ends-begin+end_str.GetLength());
	return TRUE;
}

// 解析获取段落里的数据
bool CParseHTML::GetSection2Data(CString shtml,CString& ostr)
{
	CString tqstr,wdstr,flstr;
	CString tq1,tq2,tq3,tq4,tq5,tq6;
	CString wd1,wd2,wd3;
	CString fl1,fl2,fl3;
	//获取天气段
	if (!GetHtmlSignSection(shtml,"天气概况","</tr>",tqstr))
	{
		return false;
	}
	//获取温度段
	if (!GetHtmlSignSection(shtml,"气 温","</tr>",wdstr))
	{
		return false;
	}
	//获取风力段
	if (!GetHtmlSignSection(shtml,"风向/风力","</tr>",flstr))
	{
		return false;
	}
	//解析天气段数据
	{
		std::string sstr=tqstr;
		boost::regex exampleregex("weather/.+?\\.gif");
		boost::smatch result;
		std::string::const_iterator it=sstr.begin();
		std::string::const_iterator end=sstr.end();
		CStringArray temp_ar;

		while (boost::regex_search(it,end,result, exampleregex))
		{
			CString t=result[0].str().c_str();
			temp_ar.Add(t);
			it=result[0].second;
		}
		tq1=temp_ar[0];
		tq2=temp_ar[1];
		tq3=temp_ar[2];
		tq4=temp_ar[3];
		tq5=temp_ar[4];
		tq6=temp_ar[5];
		tq1.Replace("weather/","");
		tq2.Replace("weather/","");
		tq3.Replace("weather/","");
		tq4.Replace("weather/","");
		tq5.Replace("weather/","");
		tq6.Replace("weather/","");
	}
	//解析温度
	{
		std::string sstr=wdstr;
		boost::regex exampleregex("k\">.+?<");
		boost::smatch result;
		std::string::const_iterator it=sstr.begin();
		std::string::const_iterator end=sstr.end();
		CStringArray temp_ar;

		while (boost::regex_search(it,end,result, exampleregex))
		{
			CString t=result[0].str().c_str();
			temp_ar.Add(t);
			it=result[0].second;
		}
		wd1=temp_ar[0];
		wd2=temp_ar[1];
		wd3=temp_ar[2];
		wd1.Replace("k\">","");
		wd1.Replace("<","");
		wd2.Replace("k\">","");
		wd2.Replace("<","");
		wd3.Replace("k\">","");
		wd3.Replace("<","");
	}
	//解析风力
	{
		std::string sstr=flstr;
		boost::regex exampleregex("k\">.+?</t");
		boost::smatch result;
		std::string::const_iterator it=sstr.begin();
		std::string::const_iterator end=sstr.end();
		CStringArray temp_ar;

		while (boost::regex_search(it,end,result, exampleregex))
		{
			CString t=result[0].str().c_str();
			temp_ar.Add(t);
			it=result[0].second;
		}
		fl1=temp_ar[0];
		fl2=temp_ar[1];
		fl3=temp_ar[2];
		fl1.Replace("k\">","");
		fl1.Replace("</t","");
		fl2.Replace("k\">","");
		fl2.Replace("</t","");
		fl3.Replace("k\">","");
		fl3.Replace("</t","");
	}
	ostr.Format("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",tq1,tq2,wd1,fl1,tq3,tq4,wd2,fl2,tq5,tq6,wd3,fl3);
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -