【问题标题】:Extract specific data from webpage从网页中提取特定数据
【发布时间】:2015-03-15 21:33:27
【问题描述】:

基本上这是我的代码:

int main()
{

CURL *curl;
FILE *fp;
CURLcode res; 
std::string readBuffer;
curl = curl_easy_init(); 
char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\desktop\\test.txt";
if(curl) { 
     fp = fopen(outfilename,"wb");

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com"); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "user=123&pass=123"); 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);  

    res = curl_easy_perform(curl); 
    Sleep(1000); 
    curl_easy_cleanup(curl); 
    fclose(fp);
} 

return EXIT_SUCCESS; 
}

输出成功保存在文本文件中。

我关心的是如何在特定标签之间提取特定内容。

例如,我只想要 ........ 之间的内容。

什么是最简单的方法,谢谢。

【问题讨论】:

    标签: c++ parsing curl xml-parsing libcurl


    【解决方案1】:

    在您的示例中,您正在将来自网站的响应转储到文件中,libcURL 将您点击的网页返回的数据原样写入,它不需要重构返回的数据。

    可以通过定义write_data函数来获取内存中的数据,只需要如下格式:

    size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata);

    一旦您将数据放入内存中,您就可以对其进行解析并根据需要对其进行重组。 See Example Here 使用 write_data 函数。

    对于 XML 解析,您可以使用 This sample code

    【讨论】:

    • 我已经定义好了,但是如何获取我想要的具体内容!
    • @juniorcoder 一旦你使用 libcurl 获取数据,你应该按照你需要的方式解析/格式化它,libcurl 不会对它做任何事情。如果对您有用,请检查示例解析的新链接的编辑答案。
    • @juniorcoder 我不认为,libcurl 本身不会对接收到的数据甚至要上传/发送到任何服务器的数据进行任何类型的解析/格式化。
    • herePass a char * as parameter, pointing to the full data to send in a HTTP POST operation. You must make sure that the data is formatted the way you want the server to receive it. libcurl will not convert or encode it for you in any way. For example, the web server may assume that this data is url-encoded.的描述
    • 不是我想要的完美答案,但您的 cmets 在解析问题上帮助了我很多。我将使用 Chilkat 库将 html 解析为纯文本,然后获取我想要的特定数据。谢谢。
    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 2012-07-19
    • 2015-09-16
    • 1970-01-01
    • 2022-11-18
    • 2011-10-21
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多