Annotation of embedaddon/curl/docs/examples/href_extractor.c, revision 1.1
1.1 ! misho 1: /***************************************************************************
! 2: * _ _ ____ _
! 3: * Project ___| | | | _ \| |
! 4: * / __| | | | |_) | |
! 5: * | (__| |_| | _ <| |___
! 6: * \___|\___/|_| \_\_____|
! 7: *
! 8: * Copyright (C) 2012 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
! 9: *
! 10: * This software is licensed as described in the file COPYING, which
! 11: * you should have received as part of this distribution. The terms
! 12: * are also available at https://curl.haxx.se/docs/copyright.html.
! 13: *
! 14: * You may opt to use, copy, modify, merge, publish, distribute and/or sell
! 15: * copies of the Software, and permit persons to whom the Software is
! 16: * furnished to do so, under the terms of the COPYING file.
! 17: *
! 18: * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
! 19: * KIND, either express or implied.
! 20: *
! 21: ***************************************************************************/
! 22:
! 23: /* <DESC>
! 24: * Uses the "Streaming HTML parser" to extract the href pieces in a streaming
! 25: * manner from a downloaded HTML.
! 26: * </DESC>
! 27: */
! 28: /*
! 29: * The HTML parser is found at https://github.com/arjunc77/htmlstreamparser
! 30: */
! 31:
! 32: #include <stdio.h>
! 33: #include <curl/curl.h>
! 34: #include <htmlstreamparser.h>
! 35:
! 36:
! 37: static size_t write_callback(void *buffer, size_t size, size_t nmemb,
! 38: void *hsp)
! 39: {
! 40: size_t realsize = size * nmemb, p;
! 41: for(p = 0; p < realsize; p++) {
! 42: html_parser_char_parse(hsp, ((char *)buffer)[p]);
! 43: if(html_parser_cmp_tag(hsp, "a", 1))
! 44: if(html_parser_cmp_attr(hsp, "href", 4))
! 45: if(html_parser_is_in(hsp, HTML_VALUE_ENDED)) {
! 46: html_parser_val(hsp)[html_parser_val_length(hsp)] = '\0';
! 47: printf("%s\n", html_parser_val(hsp));
! 48: }
! 49: }
! 50: return realsize;
! 51: }
! 52:
! 53: int main(int argc, char *argv[])
! 54: {
! 55: char tag[1], attr[4], val[128];
! 56: CURL *curl;
! 57: HTMLSTREAMPARSER *hsp;
! 58:
! 59: if(argc != 2) {
! 60: printf("Usage: %s URL\n", argv[0]);
! 61: return EXIT_FAILURE;
! 62: }
! 63:
! 64: curl = curl_easy_init();
! 65:
! 66: hsp = html_parser_init();
! 67:
! 68: html_parser_set_tag_to_lower(hsp, 1);
! 69: html_parser_set_attr_to_lower(hsp, 1);
! 70: html_parser_set_tag_buffer(hsp, tag, sizeof(tag));
! 71: html_parser_set_attr_buffer(hsp, attr, sizeof(attr));
! 72: html_parser_set_val_buffer(hsp, val, sizeof(val)-1);
! 73:
! 74: curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
! 75: curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
! 76: curl_easy_setopt(curl, CURLOPT_WRITEDATA, hsp);
! 77: curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
! 78:
! 79: curl_easy_perform(curl);
! 80:
! 81: curl_easy_cleanup(curl);
! 82:
! 83: html_parser_cleanup(hsp);
! 84:
! 85: return EXIT_SUCCESS;
! 86: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>