LibCurl HTML到字符串

LibCurl HTML to string

本文关键字:字符串 HTML LibCurl      更新时间:2023-10-16

我想帮助获取网站的HTML源代码并将其放入字符串中。这样我就可以搜索字符串并找到特定的文本。这些页面的链接将放在一个单独的文本文件中。

这就是我目前所拥有的:

int main(int argc, char *argv[])
{
    string line;
    ifstream myfile("profiles.txt");
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        { 
            Get_Html(line);
        }
        myfile.close();
    }
    system("PAUSE");
    return 0;
}
void Get_Html(string link)
{
    size_t found;
    CURL* curl = curl_easy_init();
    if (curl)
    {
        // Tell libcurl the URL 
        curl_easy_setopt(curl, CURLOPT_URL, link);
        // Tell libcurl what function to call when it has data 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,);
        // Do it! 
        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        if (res == 0)
        {
            found = contents.find("Currently Online");
            if (found != std::string::npos){
                cout << "Currently Online!" << endl;
            }
            found = contents.find("Currently In-Game");
            if (found != std::string::npos){
                cout << "Currently In-Game!" << endl;
            }
            found = contents.find("Offline");
            if (found != std::string::npos){
                cout << "Currently Offline!" << endl;
            }
            else
                cout << "NAH!!!" << endl;
        }
        else
            cerr << "Error: " << res << endl;
    }
}

curl_easy将把一些数据读入自己的缓冲区,并将其作为char *传递给回调。您可以获取该数据,然后将其附加到字符串中(可以将其作为用户数据传入)。将char数组附加到字符串应该非常容易。

您需要以下函数将HTML输出转换为字符串:

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}

然后在Get_Html函数中:

string response;
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response);

所以你的代码应该是这样的:

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}
int main(int argc, char *argv[])
{
    string line;
    ifstream myfile("profiles.txt");
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        { 
            Get_Html(line);
        }
        myfile.close();
    }
    system("PAUSE");
    return 0;
}
void Get_Html(string link)
{
    size_t found;
    CURL* curl = curl_easy_init();
    if (curl)
    {

        string response;
        curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string);
        curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response);
        // Tell libcurl the URL 
        curl_easy_setopt(curl, CURLOPT_URL, link);
        // Tell libcurl what function to call when it has data 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,);
        // Do it! 
        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        if (res == 0)
        {
            found = contents.find("Currently Online");
            if (found != std::string::npos){
                cout << "Currently Online!" << endl;
            }
            found = contents.find("Currently In-Game");
            if (found != std::string::npos){
                cout << "Currently In-Game!" << endl;
            }
            found = contents.find("Offline");
            if (found != std::string::npos){
                cout << "Currently Offline!" << endl;
            }
            else
                cout << "NAH!!!" << endl;
        }
        else
            cerr << "Error: " << res << endl;
    }
}