我正在寻找一个用于网络抓取的优秀 C++ 库。
它必须是 C/C++,没有别的,所以请不要将我引导到HTML 抓取选项或其他甚至没有提到 C++ 的 SO 问题/答案。
问问题
56004 次
4 回答
4
我推荐 Qt5.6.2,这个强大的库为我们提供
- 高级、直观、异步的网络 API,如 QNetworkAccessManager、QNetworkReply、QNetworkProxy 等
- 强大的正则表达式类,如 QRegularExpression
- 体面的网络引擎,如 QtWebEngine
- 像 QWidgets 一样强大、成熟的 gui
- 大部分 Qt5 api 都经过精心设计,signal 和 slot 也让编写异步代码变得更加容易
- 出色的 Unicode 支持
- 功能丰富的文件系统库。无论是创建、删除、重命名还是查找保存文件的标准路径在 Qt5 中都是小菜一碟
- QNetworkAccessManager 的异步 api 可以很容易地一次产生许多下载请求
- 跨主要桌面平台、windows、mac os 和 linux,一次编译,随处编写,一个代码库。
- 易于在 windows 和 mac 上部署(linux?也许 linuxdeployqt 可以为我们省去很多麻烦)
- 易于安装在 windows、mac 和 linux 上
- 等等
我已经用 Qt5 写了一个图像抓取应用程序,这个应用程序可以抓取几乎所有被谷歌、必应和雅虎搜索到的图像。
要了解更多细节,请访问我的 github 项目。我在我的博客上写下了关于如何通过 Qt5 抓取数据的高级概述(在堆栈溢出时发布太长)。
于 2017-05-31T08:38:03.267 回答
0
// download winhttpclient.h
// --------------------------------
#include <winhttp\WinHttpClient.h>
using namespace std;
typedef unsigned char byte;
#define foreach BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH
bool substrexvealue(const std::wstring& html,const std::string& tg1,const std::string& tg2,std::string& value, long& next) {
long p1,p2;
std::wstring wtmp;
std::wstring wtg1(tg1.begin(),tg1.end());
std::wstring wtg2(tg2.begin(),tg2.end());
p1=html.find(wtg1,next);
if(p1!=std::wstring::npos) {
p2=html.find(wtg2,next);
if(p2!=std::wstring::npos) {
p1+=wtg1.size();
wtmp=html.substr(p1,p2-p1-1);
value=std::string(wtmp.begin(),wtmp.end());
boost::trim(value);
next=p1+1;
}
}
return p1!=std::wstring::npos;
}
bool extractvalue(const std::wstring& html,const std::string& tag,std::string& value, long& next) {
long p1,p2,p3;
std::wstring wtmp;
std::wstring wtag(tag.begin(),tag.end());
p1=html.find(wtag,next);
if(p1!=std::wstring::npos) {
p2=html.find(L">",p1+wtag.size()-1);
p3=html.find(L"<",p2+1);
wtmp=html.substr(p2+1,p3-p2-1);
value=std::string(wtmp.begin(),wtmp.end());
boost::trim(value);
next=p1+1;
}
return p1!=std::wstring::npos;
}
bool GetHTML(const std::string& url,std::wstring& header,std::wstring& hmtl) {
std::wstring wurl = std::wstring(url.begin(),url.end());
bool ret=false;
try {
WinHttpClient client(wurl.c_str());
std::string url_protocol=url.substr(0,5);
std::transform(url_protocol.begin(), url_protocol.end(), url_protocol.begin(), (int (*)(int))std::toupper);
if(url_protocol=="HTTPS") client.SetRequireValidSslCertificates(false);
client.SetUserAgent(L"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
if(client.SendHttpRequest()) {
header = client.GetResponseHeader();
hmtl = client.GetResponseContent();
ret=true;
}
}catch(...) {
header=L"Error";
hmtl=L"";
}
return ret;
}
int main() {
std::string url = "http://www.google.fr";
std::wstring header,html;
GetHTML(url,header,html));
}
于 2014-10-17T16:12:18.707 回答