下載網頁源碼
阿新 • • 發佈:2019-05-11
div 字符 pub hsi 從服務器 .lib rec net readfile
#include "pch.h" #include<windows.h> #include<Wininet.h> #include<iostream> #include<fstream> #include<string> #pragma comment(lib,"WinInet.lib") using namespace std; class web { public: web(string url); ~web(); int CatchData(); private: HINTERNET hINet, hHttpFile; char szSizeBuffer[32]; DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer); string url; }; web::web(string url) { this->url = url; hINet = InternetOpen("IE6.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); //InternetOpen初始化WININET.DLL if (!hINet) { cout << "InternetOpen fail" << endl; } hHttpFile = InternetOpenUrl(hINet, url.c_str(), NULL, 0, 0, 0); //這個函數連接到一個網絡服務器上並且最被從服務器上讀取數據 if (!hHttpFile) { cout << "error open url" << endl; } } web::~web() { InternetCloseHandle(hHttpFile); //關閉句柄 InternetCloseHandle(hINet); } int web::CatchData() { BOOL bQuery = HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL); //得到關於文件的信息,將接收到的資源大小存到szSizeBuffer當中,以字節為單位。 if (bQuery == false) { InternetCloseHandle(hINet); cout << "error query info" << endl; return 3; } int FileSize = atol(szSizeBuffer); //atol函數把字符串轉換成長整型數 string revData; revData.resize(FileSize);//根據得到的網頁資源的大小信息來重新分配緩沖區字符串的大小 DWORD dwBytesRead; BOOL bRead = InternetReadFile(hHttpFile, &revData[0], FileSize, &dwBytesRead); //web瀏覽器將在InternetReadFile上循環 ,不停地從Internet上讀入數據塊。 if (!bRead) { cout << "error to read file" << endl; return 4; } ofstream out_file("test.txt"); out_file << revData; //輸出到文件 cout << "抓取成功!\n" << endl; system("pause"); return 0; } int main() { web w("https://www.sina.com.cn/"); w.CatchData(); }
下載網頁源碼