簡體   English   中英

在循環C中使用libcurl

[英]Using libcurl in loop C

我想下載一些wikitionary的頁面內容。 我在循環中使用curl。 第一次迭代是好的,但其他的給我與第一次相同的結果。 缺少什么/錯了? 謝謝。 這是循環:

std::string buffer;
   size_t curl_write( void *ptr, size_t size, size_t nmemb, void *stream)  
   {
   buffer.append((char*)ptr, size*nmemb);
   return size*nmemb;
   }
int main(int argc, char **argv)
{
CURL *curl = curl_easy_init();
string data;
data="http://fr.wiktionary.org/w/api.php?format=json&action=query&titles="; 
//Page titles  are read from local file. The code is not shown to make short.
while ( not_end_of_file){
//list_of_page_title is pages requested for the current iteration.
data=data+list_of_page_title+"prop=revisions&rvprop=content";
curl_easy_setopt(curl, CURLOPT_URL, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_perform(curl);
curl_easy_reset(curl);
}
curl_easy_cleanup(curl);
return 0;
}

我是新來的卷曲。 可能很多東西都錯過了。 感謝您的幫助。

data=data+list_of_page_title會將新標題附加到您之前的URL而不是替換之前的URL。 到最后,你將擁有一個充滿垃圾的巨大網址。 服務器可能正在關注第一個標題並忽略其余標題。

如果您只是輸出您的URL作為調試的第一步,這將是顯而易見的......“我是否在請求我認為我要求的內容?”

一個問題是您沒有重置緩沖區變量。

while ( not_end_of_file){
    buffer = ""; // reset buffer to empty string
    //list_of_page_title is pages requested for the current iteration.
    data="http://fr.wiktionary.org/w/api.php?format=json&action=query&titles=" +
        list_of_page_title +
        "prop=revisions&rvprop=content";
    curl_easy_setopt(curl, CURLOPT_URL, data.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
    curl_easy_perform(curl);
    curl_easy_reset(curl);
}

正如彼得所指出的,你對data變量的處理有一個非常類似的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM