href_extractor.c 2.6 KB

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