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++模拟简易网络爬虫实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下