C++抓网页/获取网页内容
2013-07-08 18:36
253 查看
#include<windows.h>
#include<Wininet.h>
#include<iostream>
#include<fstream>
#include<string>
#pragma comment(lib,"WinInet.lib")
using namespace std;
int main()
{
HINTERNET hINet, hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
hINet = InternetOpen("IE6.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); //InternetOpen初始化WININET.DLL
string url = "http://www.sina.com"; //抓新浪网
if ( !hINet )
{
cout << "InternetOpen fail" << endl;
return 1;
}
hHttpFile = InternetOpenUrl(hINet, url.c_str(), NULL, 0, 0, 0); //这个函数连接到一个网络服务器上并且最被从服务器上读取数据
if(!hHttpFile)
{
cout << "error open url" << endl;
return 1;
}
BOOL bQuery = HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
szSizeBuffer,
&dwLengthSizeBuffer, NULL); //得到关于文件的信息
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("duhui.txt");
out_file << revData; //输出到文件
InternetCloseHandle(hHttpFile); //关闭句柄
InternetCloseHandle(hINet);
cout << "抓取成功!/n" << endl;
system("pause");
return 0;
}
#include<Wininet.h>
#include<iostream>
#include<fstream>
#include<string>
#pragma comment(lib,"WinInet.lib")
using namespace std;
int main()
{
HINTERNET hINet, hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
hINet = InternetOpen("IE6.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); //InternetOpen初始化WININET.DLL
string url = "http://www.sina.com"; //抓新浪网
if ( !hINet )
{
cout << "InternetOpen fail" << endl;
return 1;
}
hHttpFile = InternetOpenUrl(hINet, url.c_str(), NULL, 0, 0, 0); //这个函数连接到一个网络服务器上并且最被从服务器上读取数据
if(!hHttpFile)
{
cout << "error open url" << endl;
return 1;
}
BOOL bQuery = HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
szSizeBuffer,
&dwLengthSizeBuffer, NULL); //得到关于文件的信息
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("duhui.txt");
out_file << revData; //输出到文件
InternetCloseHandle(hHttpFile); //关闭句柄
InternetCloseHandle(hINet);
cout << "抓取成功!/n" << endl;
system("pause");
return 0;
}
相关文章推荐
- C++ 与 php 的交互 之----- C++ 获取 网页文字内容,获取 php 的 echo 值。
- libcurl获取网页内容的C/C++代码
- C++写cgi 获取网页ur问号后的内容
- boost c++ lib on linux(5) - 小练习——获取网页内容并提取有用信息
- C++获取网页内容
- C++ 与 php 的交互 之----- C++ 异步获取 网页文字内容,异步获取 php 的 echo 值。
- python获取网页内容的相关编码问题
- C#获取网页内容的三种方式
- Java使用HttpClient的HttpGet获取网页内容
- C#获取网页内容的三种方式
- C#获取网页内容的三种方式
- 用TcpClient如何获取远程网页的内容
- Android WebView获取网页中JavaScript弹框内容
- 获取网页内容具体用法,瞬间可以获取视频地址或者其它有规则内容
- WebBrowser 获取网页内容
- C#获取IP归属地【使用抓取网页内容方式】
- java获取url网页指定内容
- php 获取网页内容
- 利用MFC获取网页内容
- c++运用socket获取网页源代码以及strcat与strcat_s的小差别