网页抓取选项 - C++ 仅版本

发布于 2024-07-18 07:34:57 字数 211 浏览 3 评论 0原文

我正在寻找一个好的 C++ 库来进行网页抓取。
必须是C/C++,没有其他,所以请不要引导我HTML 抓取选项 或其他 SO 问题/答案,其中甚至没有提到 C++。

I'm looking for a good C++ library for web scraping.
It has to be C/C++ and nothing else so please do not direct me to Options for HTML scraping or other SO questions/answers where C++ is not even mentioned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2024-07-25 07:34:57
柳絮泡泡 2024-07-25 07:34:57

此处使用 myhtml C/C++ 解析器; 非常简单,非常快。 除 C99 之外没有任何依赖项。 并内置 CSS 选择器(示例此处

Use myhtml C/C++ parser here; dead simple, very fast. No dependencies except C99. And has CSS selectors built in (example here)

疯了 2024-07-25 07:34:57

我推荐 Qt5.6.2,这个强大的库为我们提供了

  1. 高级、直观、异步的网络 api,如 QNetworkAccessManager、QNetworkReply、QNetworkProxy 等
  2. 强大的正则表达式类,如 QRegularExpression
  3. 体面的 Web 引擎,如 QtWebEngine
  4. 强大、成熟的 gui,如 QWidgets
  5. 大多数 Qt5 api 都设计良好、信号和槽也使编写异步代码变得更加容易
  6. 出色的 unicode 支持
  7. 功能丰富的文件系统库。 无论是创建、删除、重命名还是查找保存文件的标准路径,在 Qt5 中都是小菜一碟
  8. QNetworkAccessManager 的异步 api 可以轻松地一次生成多个下载请求
  9. 跨主要桌面平台、windows、mac os 和 linux,在任何地方编译一次即可编写,仅一个代码库。
  10. 易于在 windows 和 mac 上部署(linux?也许 linuxdeployqt 可以为我们省去很多麻烦)
  11. 易于在 windows、mac 和 linux 上安装
  12. 等等

我已经用 Qt5 编写了一个图像抓取应用程序,这个应用程序可以抓取几乎所有搜索到的图像谷歌、必应和雅虎。

要了解更多详细信息,请访问我的github项目
我写下了有关如何通过 Qt5 抓取数据的高级概述
我的博客(在堆栈溢出上发布太长了)。

I recommend Qt5.6.2, this powerful library offer us

  1. High level, intuitive, asynchronous network api like QNetworkAccessManager, QNetworkReply, QNetworkProxy etc
  2. Powerful regex class like QRegularExpression
  3. Decent web engine like QtWebEngine
  4. Robust, mature gui like QWidgets
  5. Most of the Qt5 api are well designed, signal and slot make writing asynchronous codes become much easier too
  6. Great unicode support
  7. Feature rich file system library. Whether create, remove, rename or find standard path to save files is piece of cake in Qt5
  8. Asynchronous api of QNetworkAccessManager make it easy to spawn many download request at once
  9. Cross major desktop platforms, windows, mac os and linux, write once compiled anywhere, one code bases only.
  10. Easy to deploy on windows and mac(linux?maybe linuxdeployqt can save us tons of troubles)
  11. Easy to install on windows, mac and linux
  12. And so on

I already wrote an image scraper apps by Qt5, this app can scrape almost every image searched by Google, Bing and Yahoo.

To know more details about it, please visit my github project.
I wrote down high level overview about how to scrape data by Qt5 on
my blogs(it is too long to post at stack overflow).

沦落红尘 2024-07-25 07:34:57
// 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));
}
// 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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文