i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this
Asked
Active
Viewed 280 times
-3
-
Have you tried using the native C++ file IO? Try using that and revising your answer; perhaps using a for loop to loop through all the lines of the document and append it to a string? Please do this soon so that people don't put this question on hold. – Atutouato Feb 02 '14 at 02:47
-
Alright guys, i need to give my c++ program a website (not a HTML file) and the program returns the HTML source code of this website. EX: i give www.facebook.com i get ... – Reda Feb 02 '14 at 02:50
-
@REEDOOX curl leads a good track, for what you're currently asking! – πάντα ῥεῖ Feb 02 '14 at 03:34
-
I need to get the HTML source of a webpage and put it in a string; if i give www.facebook.com i get in the string : ... in linux i run this command : curl www.facebook.com it doesnt do anything – Reda Feb 02 '14 at 03:43
1 Answers
1
Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.
-
i cant make this work , can i know what the curl http://facebook.com; if it is supposed to get the html source it doesnt :/ – Reda Feb 02 '14 at 03:21
-
1Give `exec()` a different name, it collides with the standard POSIX functions! – πάντα ῥεῖ Feb 02 '14 at 04:31