网络爬虫demo

评价:
0
(0用户)

#include “StdAfx.h”
#include “Http.h”

//构造函数
CHttp::CHttp()
{
m_bHttps = false; //默认一开始不是https协议
m_socket = INVALID_SOCKET;
}
//析构函数
CHttp::~CHttp()
{

}
//解析URL函数
bool CHttp::AnalyseURL(std::string url)
{
//https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=40770 示例 https
//http://www.163.com/ 示例 http

//将字符串分别转化为大、小写的函数
//toupper(); tolower(); 因为有些网站用的是大写的HTTPS\HTTP
std::string str = url.substr(0, 8); //substr(string, start<,length>):从string的start位置开始提取字符串,length:要提取字符串的长度
if (str.compare(“https://”)==0)
{
m_bHttps = true;
}
else if (str.find(“http://”) !=std::string::npos)
{
m_bHttps = false;
}
else
return false;

//找主机网址的反斜杠位置
int nPos = (int)url.find(‘/’, m_bHttps ? 8 : 7); //如果m_bHttps为真,那么从第8个之后的位置开始找,否则从第七个位置之后开始找
if (nPos == std::string::npos) //这句话的意思就是说如果把最后一个位置都找完了,还没找到。 npos表示string的结束位置,
{
//http://www.163.com
m_host=url.substr(m_bHttps ? 8 : 7); //例如上面这种,如果主机后面没有/,那么直接从http://开始截取,截到最后
m_object = “/”; //像上面这种没有资源路径,那我们就给他们一个斜杠
}
else
{
//如果是这种情况https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=40770
m_host = url.substr(m_bHttps ? 8 : 7, nPos – (m_bHttps ? 8 : 7));
m_object = url.substr(nPos);
}

if (m_host.empty()||m_object.empty()) //如果主机内容为空,意味着截取不到
return false;
return true;
}
//初始化网络
bool CHttp::Init()
{
if(m_socket == INVALID_SOCKET)
{
WSADATA wd;
if (0 != WSAStartup(MAKEWORD(2, 2), &wd))
return false;
if (LOBYTE(wd.wVersion) != 2 || HIBYTE(wd.wVersion) != 2) //判断请求的是不是2.2版本
return false;

//创建套接字
m_socket=socket(AF_INET, SOCK_STREAM, 0);
}
return true;
}

//初始化网络
bool CHttp::DeInit()
{
if(m_socket != INVALID_SOCKET)
{
closesocket(m_socket);
m_socket = INVALID_SOCKET;
WSACleanup();
}
return true;
}

//连接服务器
bool CHttp::Connect()
{
//将域名解析成对应的IP地址
HOSTENT * p=gethostbyname(m_host.c_str()); //P存放的内容就是由主机域名解析好后的ip地址,
if (p == NULL)
return false; //解析失败
//连接服务器
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(80);
memcpy(&sa.sin_addr, p->h_addr, 4);

if (SOCKET_ERROR == connect(m_socket, (sockaddr*)&sa, sizeof(sockaddr)))
return false;
return true;
}

bool CHttp::GetHtml(std::string& html)
{
std::string get;
std::string codename;
get += “GET “;
get += m_object;
get += ” HTTP/1.1\r\n”;
get += “Host: “;
get += m_host;
get += “\r\n”;
get += “User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37”;

get += “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”,
“Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)”,
“Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”,
“Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)”,
“Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)”,
“Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)”,
“Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)”,
“Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)”,
“Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6”,
“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1”,
“Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0”,
“Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5”,
“Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11”,
“Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20”,
“Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER”,
“Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER”,
“Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”,
“Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)”,
“Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)”,
“Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”,
“Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1”,
“Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5”,
“Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre”,
“Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0”,
“Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11”,
“Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10”;
//get += “Cookie:”\r\n”;
//get += “Host:”\r\n”;
//get += “Referer:”\r\n”;
get += “Connection: Close\r\n”;
get += “\r\n”;

//发送GET请求
if(SOCKET_ERROR==send(m_socket, get.c_str(), (int)get.length(), 0)) //套接字、发送的内容、发送多长、flag
std::cout << “GET请求发送失败” << std::endl;

//接收数据
char ch = 0;
//std::fstream dataFile; //创建一个文件,用于存放html的内容dataFile.open(“D:\\html.txt”, std::ios::out);
//codename=”E:\\project\\stklib\\Test.html”;
//dataFile.open(codename.c_str(), std::ios::out);
/*if (!dataFile)
{
printf(“文件打开失败!\n”);
return false;
}*/
while (recv(m_socket, &ch, sizeof(ch), 0))
{
html += ch;
//dataFile << ch;
}
//dataFile.close();
return true;
}

注册并通过认证的用户才可以进行评价!

admin:支持一下,感谢分享!,+10,  

发表评论