1

我想下载一些维基词典的页面内容。我在循环中使用 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;
}

我是卷曲的新手。可能很多东西都错过了。感谢您的帮助。

4

2 回答 2

3

data=data+list_of_page_title会将新标题附加到您以前的 URL 上,而不是替换以前的。到最后,您将拥有一个充满垃圾的巨大 URL。服务器可能会注意第一个标题而忽略其余部分。

如果您只是输出您的 URL 作为调试的第一步,这将是显而易见的……“我请求的是我认为我请求的内容吗?”

于 2013-11-19T18:02:49.653 回答
1

一个问题是您没有重置缓冲区变量。

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变量的处理有一个非常相似的问题。

于 2013-11-19T18:01:34.523 回答