href_extractor.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Uses the "Streaming HTML parser" to extract the href pieces in a streaming
  26. * manner from a downloaded HTML.
  27. * </DESC>
  28. */
  29. /*
  30. * The HTML parser is found at https://github.com/arjunc77/htmlstreamparser
  31. */
  32. #include <stdio.h>
  33. #include <curl/curl.h>
  34. #include <htmlstreamparser.h>
  35. static size_t write_callback(void *buffer, size_t size, size_t nmemb,
  36. void *hsp)
  37. {
  38. size_t realsize = size * nmemb, p;
  39. for(p = 0; p < realsize; p++) {
  40. html_parser_char_parse(hsp, ((char *)buffer)[p]);
  41. if(html_parser_cmp_tag(hsp, "a", 1))
  42. if(html_parser_cmp_attr(hsp, "href", 4))
  43. if(html_parser_is_in(hsp, HTML_VALUE_ENDED)) {
  44. html_parser_val(hsp)[html_parser_val_length(hsp)] = '\0';
  45. printf("%s\n", html_parser_val(hsp));
  46. }
  47. }
  48. return realsize;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. char tag[1], attr[4], val[128];
  53. CURL *curl;
  54. HTMLSTREAMPARSER *hsp;
  55. if(argc != 2) {
  56. printf("Usage: %s URL\n", argv[0]);
  57. return EXIT_FAILURE;
  58. }
  59. curl = curl_easy_init();
  60. hsp = html_parser_init();
  61. html_parser_set_tag_to_lower(hsp, 1);
  62. html_parser_set_attr_to_lower(hsp, 1);
  63. html_parser_set_tag_buffer(hsp, tag, sizeof(tag));
  64. html_parser_set_attr_buffer(hsp, attr, sizeof(attr));
  65. html_parser_set_val_buffer(hsp, val, sizeof(val)-1);
  66. curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
  67. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  68. curl_easy_setopt(curl, CURLOPT_WRITEDATA, hsp);
  69. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  70. curl_easy_perform(curl);
  71. curl_easy_cleanup(curl);
  72. html_parser_cleanup(hsp);
  73. return EXIT_SUCCESS;
  74. }