Linux C++ 简单爬虫

 

转载:http://blog.csdn.net/orthocenterchocolate/article/details/38665937

方便易用,传入URL,返回对应页面的内容

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <netdb.h>  
  4. #include <string.h>  
  5. #include <stdlib.h>  
  6. using namespace std;  
  7.   
  8. void parseHostAndPagePath(const string url, string &hostUrl, string &pagePath){  
  9.     hostUrl=url;  
  10.     pagePath="/";  
  11.     int pos=hostUrl.find("http://");  
  12.     if(-1!=pos)  
  13.         hostUrl=hostUrl.replace(pos,7,"");  
  14.     pos=hostUrl.find("https://");  
  15.     if(-1!=pos)  
  16.         hostUrl=hostUrl.replace(pos,8,"");  
  17.     pos=hostUrl.find("/");  
  18.     if(-1!=pos){  
  19.         pagePath=hostUrl.substr(pos);  
  20.         hostUrl=hostUrl.substr(0,pos);  
  21.     }  
  22. }  
  23.   
  24. string getPageContent(const string url){  
  25.     struct hostent *host;  
  26.     string hostUrl, pagePath;  
  27.     parseHostAndPagePath(url, hostUrl, pagePath);  
  28.     if(0==(host=gethostbyname(hostUrl.c_str()))){  
  29.         cout<<"gethostbyname error\n"<<endl;  
  30.         exit(1);  
  31.     }  
  32.   
  33.     struct sockaddr_in pin;  
  34.     int port=80;  
  35.     bzero(&pin,sizeof(pin));  
  36.     pin.sin_family=AF_INET;  
  37.     pin.sin_port=htons(port);  
  38.     pin.sin_addr.s_addr=((struct in_addr*)(host->h_addr))->s_addr;  
  39.     int isock;  
  40.     if((isock = socket(AF_INET, SOCK_STREAM, 0))==-1){  
  41.         cout<<"open socket error\n"<<endl;  
  42.         exit(1);  
  43.     }  
  44.   
  45.     string requestHeader;  
  46.     requestHeader="GET "+pagePath+" HTTP/1.1\r\n";  
  47.     requestHeader+="Host: "+hostUrl+"\r\n";  
  48.     requestHeader+="Accept: */*\r\n";  
  49.     requestHeader+="User-Agent: Mozilla/4.0(compatible)\r\n";  
  50.     requestHeader+="connection:Keep-Alive\r\n";  
  51.     requestHeader+="\r\n";  
  52.   
  53.     if(connect(isock, (const sockaddr*)&pin, sizeof(pin))==-1){  
  54.         cout<<"connect error\n"<<endl;  
  55.         exit(1);  
  56.     }  
  57.     if(send(isock, requestHeader.c_str(), requestHeader.size(), 0)==-1){  
  58.         cout<<"send error\n"<<endl;  
  59.         exit(1);  
  60.     }  
  61.   
  62.     struct timeval timeout={1,0};  
  63.     setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));  
  64.     char c;  
  65.     bool flag=true;  
  66.     while(recv(isock, &c, 1, 0)>0){  
  67.         if('\r'==c){  
  68.             continue;  
  69.         }else if('\n'==c){  
  70.             if(false==flag)  
  71.                 break;  
  72.             flag=false;  
  73.         }else{  
  74.             flag=true;  
  75.         }  
  76.     }  
  77.   
  78.     int len, BUFFER_SIZE=512;  
  79.     char buffer[BUFFER_SIZE];  
  80.     string pageContent="";  
  81.     while((len = recv(isock, buffer, BUFFER_SIZE-1, 0))>0){  
  82.         buffer[len]='\0';  
  83.         pageContent+=buffer;  
  84.     }  
  85.   
  86.     return pageContent;  
  87. }  
  88.   
  89. int main()  
  90. {  
  91.     cout<<getPageContent("http://www.hao123.com")<<endl;  
  92.     return 0;  
  93. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值