如何读取网页内表格数据
如何读取网页内表格数据?
[解决办法]
自己分析HTML <table>标签内的数据就是了!
[解决办法]
可以使用idhttp这个控件。
[解决办法]
网页地址给出
大家帮你试试
[解决办法]
这是我做的一个小程序,跟你要的差不多,自己体会
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "GetRl1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool get=false;
TStringList *log=new TStringList();
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Text= IdHTTP1->Get("http://www.pearlwater.gov.cn/ztzl/fxzt/sqcx.jsp");
StatusBar1->SimpleText="正在读取网页数据......";
// Idglobal::Sleep(1000);
String dt=Now().FormatString("yyyy")+"年"+Now().FormatString("m")+"月"+Now().FormatString("d")+"日8时";
String lz="柳州";
String sw="水位";
StatusBar1->SimpleText="已读取"+IntToStr(Memo1->Lines->Count)+"行网页数据......";
for(int i=0;i<Memo1->Lines->Count;i++)
{
StatusBar1->SimpleText="正在处理网页数据......"+IntToStr(i);
if(Memo1->Lines->Strings[i].Pos(lz)!=0 && Memo1->Lines->Strings[i].Pos(dt)!=0 && Memo1->Lines->Strings[i].Pos(sw)!=0)
{
StatusBar1->SimpleText="已找到水位数据......";
float lv=Memo1->Lines->Strings[i].SubString(Memo1->Lines->Strings[i].Pos(sw)+6,5).ToDouble() ;
SaveToDb(lv);
StatusBar1->SimpleText="水位数据已保存到数据库中......";
log->Add("保存水位时间:"+DateTimeToStr(Now()));
log->SaveToFile("log.txt");
get=true;
break;
}
}
StatusBar1->SimpleText="已处理完网页数据......";
}
//---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
ADOQuery1->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liuzhou.mdb;Persist Security Info=False";
String sql="select datetime as 日期时间,shici as 时次,validtime as 时效,int(liuzhou*100+0.5)/100.0 as 柳江水位 from dbo_gxmyl_river_level where validtime=0 order by datetime desc";
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sql) ;
ADOQuery1->Open();
// DateTimePicker1->Date=Date();
if(FileExists("log.txt"))
log->LoadFromFile("log.txt");
log->Add("运行程序时间:"+DateTimeToStr(Now()));
log->SaveToFile("log.txt");
}
//---------------------------------------
void __fastcall TForm1::SaveToDb(double lv)
{
String sql="select * from dbo_gxmyl_river_level where validtime=0 and datetime<=#" +DateToStr(Date()+1)+"# and datetime>#" +DateToStr(Date())+"#";
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sql) ;
ADOQuery1->Open();
if(ADOQuery1->RecordCount>0)
{
/* if(MessageDlg(DateToStr(DateTimePicker1->Date)+"日的水位:"+FloatToStr(int(ADOQuery1->FieldByName("liuzhou")->AsFloat*100+0.5)/100.0)+"已存在,覆盖吗?" , mtWarning, TMsgDlgButtons() << mbOK<<mbCancel, 0)!=mrOk)
{
DateTimePicker1->Date=DateTimePicker1->Date+1;
return;
}
else
*/
ADOQuery1->Delete();
}
ADOQuery1->Insert();
ADOQuery1->FieldByName("datetime")->AsDateTime=StrToDateTime(DateToStr(Date())+" 08:00:00");
ADOQuery1->FieldByName("shici")->AsInteger=0;
ADOQuery1->FieldByName("validtime")->AsInteger=0;
ADOQuery1->FieldByName("liuzhou")->AsFloat=lv;
ADOQuery1->UpdateRecord();
ADOQuery1->Post() ;
sql=sql="select datetime as 日期时间,shici as 时次,validtime as 时效,int(liuzhou*100+0.5)/100.0 as 柳江水位 from dbo_gxmyl_river_level where validtime=0 order by datetime desc";
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(sql) ;
ADOQuery1->Open();
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(Time()>StrToTime("08:00:00"))
{
if(!get)
Button1Click(Sender);
else
StatusBar1->SimpleText="已下载今日水位数据,现在停止搜索,等待下载明日数据,请不要关闭...";
}
else
{
StatusBar1->SimpleText="资料末上网时间末到,停止搜索......";
get=false;
}
}
//---------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
log->Add("关闭程序时间:"+DateTimeToStr(Now()));
log->SaveToFile("log.txt");
}
//---------------------------------------