方便易用,传入URL,返回对应页面的内容
#include <iostream>
#include <string>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
void parseHostAndPagePath(const string url, string &hostUrl, string &pagePath){
hostUrl=url;
pagePath="/";
int pos=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);
}
}
string getPageContent(const string url){
struct hostent *host;
string hostUrl, pagePath;
parseHostAndPagePath(url, hostUrl, pagePath);
if(0==(host=gethostbyname(hostUrl.c_str()))){
cout<<"gethostbyname error\n"<<endl;
exit(1);
}
struct sockaddr_in pin;
in

本文介绍如何在Linux系统中使用C++编程语言实现一个简单的网络爬虫,通过socket通信获取指定URL的网页内容。
最低0.47元/天 解锁文章
699

被折叠的 条评论
为什么被折叠?



