• c++ 实现https网页上的图片爬取


    一.主要的原理

      我们通过发送一个http请求,获得目标网页的html源代码,然后通过正则表达式获取到图片的URL,把该网页的所有的图片都保存到一个文件夹,这就是整个软件的流程。

    二.具体的实践

      现在很多的网站都是https协议但是有一部分还是http协议,其实https就是http协议的安全版本,相当于http+ssl,SSL是介于HTTP应用层和TCP传输层,和HTTP相比HTTPS发送数据需要SSL加密,然后发送。所以说我们想通过https协议发送数据给服务器,需要经历一下这几个步骤:

    首先我们先和服务器进行socket连接,然后将SSL和创建的socket套接字进行绑定,之后我们发送数据都是通过SSL发送即可,下面介绍一下具体的流程:

    首先我们先进行socket连接:

    //建立TCP连接
    bool Connect()
    {
    	//初始化套接字
    	WSADATA wsadata;
    	if (0 != WSAStartup(MAKEWORD(2, 2), &wsadata)) return false;
    
    	//创建套接字
    	g_sock = socket(AF_INET, SOCK_STREAM, 0);
    	if (g_sock == INVALID_SOCKET) return false;
    
    	//将域名转换为IP地址
    	hostent *p = gethostbyname(g_Host);
    	if (p == NULL) return false;
    
    	sockaddr_in sa;
    	memcpy(&sa.sin_addr, p->h_addr, 4);
    	sa.sin_family = AF_INET;
    	sa.sin_port = htons(443);
    
    	if (SOCKET_ERROR == connect(g_sock, (sockaddr*)&sa, sizeof(sockaddr))) return false;
    	return true;
    }
    

      

    HTTPS=HTTP + SSL,因此利用OpenSSL发送请求给HTTPS站点和第二章的SOCKET发送HTTP是非常相似的,只不过要在原生的套接字上套上SSL层,基本流程如下:

    a. WSAStartup对Winsock服务进行初始化

    b. 建立socket套接字

    c. connect连接服务端

    d. 建立SSL上下文

    e. 建立SSL

    f. 将SSL与前面建立的socket套接字绑定

    g. SSL_write()发送数据

    h. SSL_read()接收数据

    bool SSL_Connect()
    {
    	// Register the error strings for libcrypto & libssl
    
    	ERR_load_BIO_strings();
    	// SSl库的初始化,载入SSL的所有算法,载入所有的SSL错误信息
    	SSL_library_init();
    	OpenSSL_add_all_algorithms();
    	SSL_load_error_strings();
    
    	// New context saying we are a client, and using SSL 2 or 3
    	sslContext = SSL_CTX_new(SSLv23_client_method());
    	if (sslContext == NULL)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Create an SSL struct for the connection
    	sslHandle = SSL_new(sslContext);
    	if (sslHandle == NULL)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Connect the SSL struct to our connection
    	if (!SSL_set_fd(sslHandle, g_sock))
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Initiate SSL handshake
    	if (SSL_connect(sslHandle) != 1)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    
    	return true;
    }

    三.遇到的问题

    1.首先VS控制台的编码方式是GBK的方式,但是有的网页就是UTF-8所以我们要进行这方面的转换

    string UtfToGbk(const char* utf8)
    {
    	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    	wchar_t* wstr = new wchar_t[len + 1];
    	memset(wstr, 0, len + 1);
    	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    	char* str = new char[len + 1];
    	memset(str, 0, len + 1);
    	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    	if (wstr) delete[] wstr;
    	return str;
    }
    

    2.OPENSSL的安装

      网上有很多自己下载源码然后自己编译的,但是在这个项目中我们可以直接使用别人编译好的库不用自己进行编译,直接在VS的项目目录中添加即可

    3.还有就是图片的下载,是这种LPCWSTR数据类型的,我们通过string转换成LPCWSTR(不知道为什么URLDownloadToFile里面的两个都要这样进行转换不能用其他的函数)

    		string savepath = "E:\c++_file\网络爬虫1\网络爬虫\网络爬虫\img\"+to_string(i)+".jpg";
    		size_t len1 = savepath.length();
    		wchar_t* imgsavepath = new wchar_t[len1];
    		int nmlen1 = MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, imgsavepath, len1);

     这是完整的下载的代码:

               //URL生成
    		per = mat[1].str();
    		size_t len = per.length();//获取字符串长度
    		int nmlen = MultiByteToWideChar(CP_ACP, 0, per.c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零 //返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
    		wchar_t* buffer = new wchar_t[nmlen];
    		MultiByteToWideChar(CP_ACP, 0, per.c_str(), len + 1, buffer, nmlen);
    		//保存路径
    		string savepath = "E:\c++_file\网络爬虫1\网络爬虫\网络爬虫\img\"+to_string(i)+".jpg";
    		size_t len1 = savepath.length();
    		wchar_t* imgsavepath = new wchar_t[len1];
    		int nmlen1 = MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, imgsavepath, len1);
    		cout << mat.str() << endl;
    		cout << savepath << endl;
    		//下载文件
    		HRESULT hr = URLDownloadToFile(NULL, buffer, imgsavepath, 0, NULL);
    		if (hr == S_OK)
    		{
    			cout << "-------ok" << endl;
    		}  

     四.完整代码

    #include "spider.h"
    
    
    
    int main()
    {
    	cout << "*********************************************************" << endl;
    	cout << "***********************爬取图片系统***********************" << endl;
    	cout << "*********************************************************" << endl;
    
    	//创建图片的储存的目录
    	CreateDirectory(L"./img", NULL);
    
    	//开始抓取
    	string starturl = "https://www.shiyanlou.com/#sign-modal";
    
    	StartCatch(starturl);
    	//while (1);
    	return 0;
    }
    
    
    void StartCatch(string startUrl)
    {
    	queue<string> q;
    	q.push(startUrl);
    
    	while (!q.empty())
    	{
    		string cururl = q.front();
    		q.pop();
    
    		//解析URL
    		if (false == Analyse(cururl))
    		{
    			cout << "解析URL失败,错误码:" << GetLastError() << endl;
    			continue;
    		}
    
    		//连接服务器
    		if (false == Connect())
    		{
    			cout << "连接服务器失败,错误代码:" << GetLastError() << endl;
    			continue;
    		}
    
    		//建立ssl连接
    		if (false == SSL_Connect())
    		{
    			cout << "建立SSL连接失败,错误代码:" << GetLastError() << endl;
    			continue;
    		}
    
    		//获取网页
    		string html;
    		if (false == Gethtml(html))
    		{
    			cout << "获取网页数据失败,错误代码:" << GetLastError() << endl;
    			continue;
    		}
    		if (false == RegexIamage(html))
    		{
    			cout << "获取网页数据失败,错误代码:" << GetLastError() << endl;
    			continue;
    		}
    		//cout << html << endl;
    	}
    	//释放
    	SSL_shutdown(sslHandle);
    	SSL_free(sslHandle);
    	SSL_CTX_free(sslContext);
    	closesocket(g_sock);
    	WSACleanup();
    }
    
    
    //解析url
    bool Analyse(string url)
    {
    	char *pUrl = new char[url.length() + 1];
    	strcpy(pUrl, url.c_str());
    
    	char *pos = strstr(pUrl, "https://");//找到http://开头的字符串
    	if (pos == NULL) return false;
    	else pos += 8;//将http://开头省略
    
    	sscanf(pos, "%[^/]%s", g_Host, g_Object);
    
    	delete[] pUrl;
    	return true;
    }
    
    
    //建立TCP连接
    bool Connect()
    {
    	//初始化套接字
    	WSADATA wsadata;
    	if (0 != WSAStartup(MAKEWORD(2, 2), &wsadata)) return false;
    
    	//创建套接字
    	g_sock = socket(AF_INET, SOCK_STREAM, 0);
    	if (g_sock == INVALID_SOCKET) return false;
    
    	//将域名转换为IP地址
    	hostent *p = gethostbyname(g_Host);
    	if (p == NULL) return false;
    
    	sockaddr_in sa;
    	memcpy(&sa.sin_addr, p->h_addr, 4);
    	sa.sin_family = AF_INET;
    	sa.sin_port = htons(443);
    
    	if (SOCKET_ERROR == connect(g_sock, (sockaddr*)&sa, sizeof(sockaddr))) return false;
    	return true;
    }
    
    
    bool SSL_Connect()
    {
    	// Register the error strings for libcrypto & libssl
    
    	ERR_load_BIO_strings();
    	// SSl库的初始化,载入SSL的所有算法,载入所有的SSL错误信息
    	SSL_library_init();
    	OpenSSL_add_all_algorithms();
    	SSL_load_error_strings();
    
    	// New context saying we are a client, and using SSL 2 or 3
    	sslContext = SSL_CTX_new(SSLv23_client_method());
    	if (sslContext == NULL)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Create an SSL struct for the connection
    	sslHandle = SSL_new(sslContext);
    	if (sslHandle == NULL)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Connect the SSL struct to our connection
    	if (!SSL_set_fd(sslHandle, g_sock))
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    	// Initiate SSL handshake
    	if (SSL_connect(sslHandle) != 1)
    	{
    		ERR_print_errors_fp(stderr);
    		return false;
    	}
    
    	return true;
    }
    
    bool Gethtml(string & html)
    {
    	char temp1[100];
    	sprintf(temp1, "%d", 166);
    	string c_get;   
    	c_get = c_get
    		+ "GET " + g_Object + " HTTP/1.1
    "
    		+ "Host: " + g_Host + "
    "
    		+ "Content-Type: text/html; charset=UTF-8
    "
    		//+ "Content-Length:" + temp1 + "
    "
    		//+ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
    "
    		+ "Connection:Close
    
    ";
    		//+ temp;
    
    	SSL_write(sslHandle, c_get.c_str(), c_get.length());
    
    	char buff[101];
    	int nreal = 0;
    
    	while ((nreal = SSL_read(sslHandle, buff, 100)) > 0)
    	{
    		buff[nreal] = '';
    		html += UtfToGbk(buff);
    		//printf("%s
    ", buff);
    		memset(buff, 0, sizeof(buff));
    	}
    	//printf("%s
    ", html);
    	return true;
    }
    
    string UtfToGbk(const char* utf8)
    {
    	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    	wchar_t* wstr = new wchar_t[len + 1];
    	memset(wstr, 0, len + 1);
    	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    	char* str = new char[len + 1];
    	memset(str, 0, len + 1);
    	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    	if (wstr) delete[] wstr;
    	return str;
    }
    
    
    bool RegexIamage(string & html)
    {
    	smatch mat;
    	regex rgx("src="(.*(png|svg|jpg))"");
    	string::const_iterator start = html.begin();
    	string::const_iterator end = html.end();
    	string per;
    	int i = 1;
    	while (regex_search(start, end, mat, rgx))
    	{
    		//URL生成
    		per = mat[1].str();
    		size_t len = per.length();//获取字符串长度
    		int nmlen = MultiByteToWideChar(CP_ACP, 0, per.c_str(), len + 1, NULL, 0);//如果函数运行成功,并且cchWideChar为零 //返回值是接收到待转换字符串的缓冲区所需求的宽字符数大小。
    		wchar_t* buffer = new wchar_t[nmlen];
    		MultiByteToWideChar(CP_ACP, 0, per.c_str(), len + 1, buffer, nmlen);
    		//保存路径
    		string savepath = "E:\c++_file\网络爬虫1\网络爬虫\网络爬虫\img\"+to_string(i)+".jpg";
    		size_t len1 = savepath.length();
    		wchar_t* imgsavepath = new wchar_t[len1];
    		int nmlen1 = MultiByteToWideChar(CP_ACP, 0, savepath.c_str(), len1 + 1, imgsavepath, len1);
    		cout << mat.str() << endl;
    		cout << savepath << endl;
    		//下载文件
    		HRESULT hr = URLDownloadToFile(NULL, buffer, imgsavepath, 0, NULL);
    		if (hr == S_OK)
    		{
    			cout << "-------ok" << endl;
    		}
    		start = mat[0].second;
    		i++;	
    	}
    	return true;
    }
    
    LPCWSTR stringToLPCWSTR(string orig)
    {
    	size_t origsize = orig.length() + 1;
    	const size_t newsize = 100;
    	size_t convertedChars = 0;
    	wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t) *(orig.length() - 1));
    	mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
    
    
    	return wcstring;
    }
    

      下面是头文件:

    #pragma once
    
    #include <iostream>
    #include <Windows.h>
    #include <string>
    #include <queue>
    #include <regex>
    #include <urlmon.h>
    
    #include <openssl/rand.h>
    #include <openssl/ssl.h>
    #include <openssl/err.h>
    
    #pragma comment(lib, "urlmon.lib")
    #pragma comment( lib, "libeay32.lib" )
    #pragma comment( lib, "ssleay32.lib" )
    
    #pragma comment(lib, "WS2_32")  // 链接到WS2_32.lib 
    
    using namespace std;
    
    char g_Host[MAX_PATH];
    char g_Object[MAX_PATH];
    
    SOCKET g_sock;
    SSL *sslHandle;
    SSL_CTX *sslContext;
    BIO * bio;
    
    
    //开始抓取
    void StartCatch(string startUrl);
    //解析URL
    bool Analyse(string url);
    //连接服务器
    bool Connect();
    //建立SSl连接
    bool SSL_Connect();
    //得到html
    bool Gethtml(string& html);
    //UTF转GBK
    std::string UtfToGbk(const char* utf8);
    //正则表达式
    bool RegexIamage(string & html);
    
    LPCWSTR stringToLPCWSTR(std::string orig);
    

      

  • 相关阅读:
    doges
    Unity Fps示例
    使用Unity的2D功能开发弹球游戏
    Unity UGUI 原理篇(二):Canvas Scaler 縮放核心
    UGUI 深度優化提升手遊效能
    关于Unity中的UGUI优化,你可能遇到这些问题
    git branch --set-upstream 本地关联远程分支
    git rm 与 git reset
    Git笔记之初识vi编辑器
    [内容分享]粗略判断Shader每条代码的成本
  • 原文地址:https://www.cnblogs.com/yskn/p/9552981.html
Copyright © 2020-2023  润新知