href_extractor.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 /* <DESC>
00024  * Uses the "Streaming HTML parser" to extract the href pieces in a streaming
00025  * manner from a downloaded HTML.
00026  * </DESC>
00027  */
00028 /*
00029  * The HTML parser is found at http://code.google.com/p/htmlstreamparser/
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 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04