这是一个小程序,我提取&保存/写在一个文本文件中的Facebook帐户源代码。您可以根据需要更改它(您可以将“http://www.facebook.com”更改为“http://www.google.com/”)。还请记住将wininet.a(库)链接到您的项目。希望它会有所帮助:)
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <conio.h>
#include <fstream.h>
fstream fs_obj;
using namespace std;
int main(int argc, char *argv[])
{
fs_obj.open("temp.txt",ios::out | ios::app);
HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnection = InternetConnectA(hInternet, "www.facebook.com", 80, " "," ", INTERNET_SERVICE_HTTP, 0, 0); //enter url here
HINTERNET hData = HttpOpenRequestA(hConnection, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
char buf[ 2048 ] ;
HttpSendRequestA(hData, NULL, 0, NULL, 0) ;
string total;
DWORD bytesRead = 0 ;
DWORD totalBytesRead = 0 ;
while(InternetReadFile(hData, buf, 2000, &bytesRead) && bytesRead != 0)
{
buf[ bytesRead ] = 0 ; // insert the null terminator.
total=total+buf;
printf("%d bytes read\n", bytesRead) ;
totalBytesRead += bytesRead ;
}
fs_obj<<total<<"\n--------------------end---------------------\n";
fs_obj.close();
printf("\n\n END -- %d bytes read\n", bytesRead) ;
printf("\n\n END -- %d TOTAL bytes read\n", totalBytesRead) ;
cout<<endl<<total<<endl; //it will save source code to (temp.txt) file
InternetCloseHandle(hData) ;
InternetCloseHandle(hConnection) ;
InternetCloseHandle(hInternet) ;
system("pause");
}
用temp.html重命名temp.txt,用浏览器打开它,你会得到那个网页。
试试'libcurl'。 – 2012-02-11 23:33:08
@LightnessRacesinOrbit不,但是当它抓取这个问题时它会被郁闷:) – sehe 2012-02-11 23:36:10
@sehe:对, – 2012-02-11 23:36:28