以前就想过做象bbs那样的天气预报的东西,
一直懒惰,这些天pkuxkx.org 5555开张,
想着送点程序吧,就弄了这么一个东西,
不过我不会正则分析那一套,所以智能
针对一个站点的一个页面做,呵呵。
//getweb_d.c
//从网络上抓取信息,
//本例子为抓取来自:"202.106.184.193"的天气预报信息。
#include < net/socket.h >
#include
#define SITE "202.106.184.193"
inherit F_DBASE;
int have_get=0;
int fail=0;
string msg="";
int check_time();
void close(int);
void write_s(int);
void create()
{
set("channel_id","天气预报");
seteuid(getuid());
check_time();
}
int check_time()
{
mixed* times=localtime(time());
int hours=times[2];
int fd,fc;
if(hours==3 && ! have_get)//每天3点开始抓网页
{
if(fd=socket_create(STREAM,"read_callback")<0)
{
write("socket_create error "+fd+"\n");
return 1;
}
if((fc=socket_connect(fd,SITE+" 80","read_callback","write_callback
"))!= EESUCCESS)
{
close(fd);
write("socket_connect error "+fc+"\n");
return 1;
}
remove_call_out("write_s");
call_out("write_s",2,fd);
remove_call_out("close");
call_out("close",20,fd);//等待二十秒,过时认为连接失败。
}
if(hours==12)
{
have_get=0;//12点清除标记
msg="";
fail=0;
}
remove_call_out("check_time");
call_out("check_time",15*60);
return 1;
}
void read_callback(int fd,mixed val)
{
msg+=sprintf("%O",val);
}
void write_callback(int fd)
{
return;
}
void close(int fd)
{
string sun;
string gaowen;
string diwen;
string fengli;
write("close start\n");
if(strsrch(msg,"HTTP/1.1 200 OK")!=-1) //成功读取
{
//这些字符串分析与具体网页相关。
int index,index2;
string* msgArray=({});
index=strsrch(msg,"北京");
msg=msg[index..sizeof(msg)];
index2=strsrch(msg,"");
msg=msg[0..index2-1];
msg=replace_string(msg,"/","");
msgArray=explode(msg,"
尊重作者 转载请注明出处52mud.com