我想下载一些维基词典的页面内容。我在循环中使用 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;
}
我是 curl 的新手。可能错过了很多东西。感谢您的帮助。
最佳答案
data=data+list_of_page_title
会将新标题附加到您之前的 URL 而不是替换之前的。到最后,您将拥有一个充满垃圾的巨大 URL。服务器可能会注意第一个标题而忽略其余部分。
如果您只是在调试的第一步输出您的 URL,这将是显而易见的……“我请求的是我认为我请求的吗?”
关于c++ - 在循环 C 中使用 libcurl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20078400/