linux c++模拟简易网络爬虫实例
废话不多说,直接上代码
/* *Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties. *Tochangethistemplatefile,chooseTools|Templates *andopenthetemplateintheeditor. */ /* *File:main.cpp *Author:yangchao * */ #include#include #include #include #include usingnamespacestd; voidparseHostAndPagePath(conststringurl,string&hostUrl,string&pagePath){ hostUrl=url; pagePath="/"; intpos=hostUrl.find("http://"); if(-1!=pos) hostUrl=hostUrl.replace(pos,7,""); pos=hostUrl.find("https://"); if(-1!=pos) hostUrl=hostUrl.replace(pos,8,""); pos=hostUrl.find("/"); if(-1!=pos) { pagePath=hostUrl.substr(pos); hostUrl=hostUrl.substr(0,pos); } } stringgetPageContent(conststringurl){ structhostent*host; stringhostUrl,pagePath; parseHostAndPagePath(url,hostUrl,pagePath); if(0==(host=gethostbyname(hostUrl.c_str()))) { cout<<"gethostbynameerror\n"< h_addr))->s_addr; intisock; if((isock=socket(AF_INET,SOCK_STREAM,0))==-1) { cout<<"opensocketerror\n"< 0){ if('\r'==c){ continue; }elseif('\n'==c){ if(false==flag) break; flag=false; }else{ flag=true; } } intlen,BUFFER_SIZE=512; charbuffer[BUFFER_SIZE]; stringpageContent=""; while((len=recv(isock,buffer,BUFFER_SIZE-1,0))>0){ buffer[len]='\0'; pageContent+=buffer; } returnpageContent; } intmain(intargc,char**argv){ cout< 以上这篇linuxc++模拟简易网络爬虫实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。