【发布时间】:2018-08-17 15:34:55
【问题描述】:
使用 C++ 和 libcurl 库,我试图以这种方式从该网站https://www.nutritionix.com/food/Banana 获取完整的 HTML:
int main(){
std::string content;
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = nullptr;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.nutritionix.com/food/Banana" );
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
CURLcode code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
std::cout << content << std::endl;
system("pause");
}
writer函数是这样定义的:
static int writer(char *data, size_t size, size_t nmemb, std::string *writerData) {
if (writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
通过这种方式,我只能获取少量 HTML 代码,但是如何检索完整的 HTML 内容以在稍后阶段对其进行解析?
【问题讨论】:
-
试试
http://,否则你需要SSL支持。 -
好的,谢谢您的回答。无论如何,使用
http://,我得到的更少。 -
@nicoperillo 我用完整的解释和示例更新了我的答案
-
@Axalo 我刚刚编辑了我的代码,现在还有一个
CURLOPT_FOLLOWLOCATION。这是你的意思吗?无论如何结果不会改变。
标签: c++ screen-scraping libcurl