Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <stdio.h>
00033 #include <curl/curl.h>
00034 #include <htmlstreamparser.h>
00035
00036
00037 static size_t write_callback(void *buffer, size_t size, size_t nmemb,
00038 void *hsp)
00039 {
00040 size_t realsize = size * nmemb, p;
00041 for(p = 0; p < realsize; p++) {
00042 html_parser_char_parse(hsp, ((char *)buffer)[p]);
00043 if(html_parser_cmp_tag(hsp, "a", 1))
00044 if(html_parser_cmp_attr(hsp, "href", 4))
00045 if(html_parser_is_in(hsp, HTML_VALUE_ENDED)) {
00046 html_parser_val(hsp)[html_parser_val_length(hsp)] = '\0';
00047 printf("%s\n", html_parser_val(hsp));
00048 }
00049 }
00050 return realsize;
00051 }
00052
00053 int main(int argc, char *argv[])
00054 {
00055 char tag[1], attr[4], val[128];
00056 CURL *curl;
00057 HTMLSTREAMPARSER *hsp;
00058
00059 if(argc != 2) {
00060 printf("Usage: %s URL\n", argv[0]);
00061 return EXIT_FAILURE;
00062 }
00063
00064 curl = curl_easy_init();
00065
00066 hsp = html_parser_init();
00067
00068 html_parser_set_tag_to_lower(hsp, 1);
00069 html_parser_set_attr_to_lower(hsp, 1);
00070 html_parser_set_tag_buffer(hsp, tag, sizeof(tag));
00071 html_parser_set_attr_buffer(hsp, attr, sizeof(attr));
00072 html_parser_set_val_buffer(hsp, val, sizeof(val)-1);
00073
00074 curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
00075 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
00076 curl_easy_setopt(curl, CURLOPT_WRITEDATA, hsp);
00077 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
00078
00079 curl_easy_perform(curl);
00080
00081 curl_easy_cleanup(curl);
00082
00083 html_parser_cleanup(hsp);
00084
00085 return EXIT_SUCCESS;
00086 }