html链接提取/c++
bool fetchHref(const char * html) { const char *p=html; const char *p1=NULL; const char *p2="m"; const char *aneedle="<a"; const char *mneedle="<map"; const char *hneedle="href"; const char *start=0; int len; char href[BUFSIZ]; int count=0; if(!p||!*p) return true; while(*p){ p1=strstr(p,aneedle); if(p2) p2=strstr(p,mneedle); //一般没有 可以在此提高效率 if(!p2&&p1) p=p1; //顺序 效率 else if(!p1&&!p2) return true; else if(!p1&&p2) p=p2; else p=p1>p2?p2:p1; p=strstr(p,hneedle); if(!p) return true; while(*p&&*p!='='&&*p!='>') p++; //find '=' if(*p++=='='){ while (*p&&(*p==' '||*p=='\"'||*p=='\'')) p++; //find start if(*p&&*p!='>'){ start=p; len=0; while (*p&&*p!=' '&&*p!='\"'&&*p!='\''&&*p!='>'){p++; len++;} //find end if(*p){ memset(href,0,BUFSIZ); strncpy(href,start,len); printf("%dhref=%s\n",++count,href); fs<<href<<endl; } } } } }
|