LibCurl HTML в строку

0

Я хотел бы помочь в получении 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;
    }

}
  • 0
    Ваша настройка CURLOPT_WRITEFUNCTION не завершена. Вам нужна помощь? Что-то другое?
  • 0
    Это та часть, в которой я хотел бы помочь. Примеры, на которые я смотрел, берутся в char *. Есть ли способ, которым я могу преобразовать строку в символ *? Так как я беру строку из файла .txt. (URL сайта)
Показать ещё 2 комментария
Теги:
libcurl

2 ответа

0
Лучший ответ

curl_easy собирается прочитать некоторые данные в свой собственный буфер и передать это вашему обратному вызову как char *. Вы можете взять эти данные, а затем добавить их в свою строку (которую вы можете передать как пользовательские данные). Добавление массива символов в строку должно быть очень простым.

0

вам понадобится следующая функция, чтобы включить вывод 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;
    }

} 

Ещё вопросы

Сообщество Overcoder
Наверх
Меню