find_links.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2013 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: jdtang@google.com (Jonathan Tang)
  16. //
  17. // Finds the URLs of all links in the page.
  18. #include <stdlib.h>
  19. #include <fstream>
  20. #include <iostream>
  21. #include <string>
  22. #include "gumbo.h"
  23. static void search_for_links(GumboNode* node) {
  24. if (node->type != GUMBO_NODE_ELEMENT) {
  25. return;
  26. }
  27. GumboAttribute* href;
  28. if (node->v.element.tag == GUMBO_TAG_A &&
  29. (href = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
  30. std::cout << href->value << std::endl;
  31. }
  32. GumboVector* children = &node->v.element.children;
  33. for (unsigned int i = 0; i < children->length; ++i) {
  34. search_for_links(static_cast<GumboNode*>(children->data[i]));
  35. }
  36. }
  37. int main(int argc, char** argv) {
  38. if (argc != 2) {
  39. std::cout << "Usage: find_links <html filename>.\n";
  40. exit(EXIT_FAILURE);
  41. }
  42. const char* filename = argv[1];
  43. std::ifstream in(filename, std::ios::in | std::ios::binary);
  44. if (!in) {
  45. std::cout << "File " << filename << " not found!\n";
  46. exit(EXIT_FAILURE);
  47. }
  48. std::string contents;
  49. in.seekg(0, std::ios::end);
  50. contents.resize(in.tellg());
  51. in.seekg(0, std::ios::beg);
  52. in.read(&contents[0], contents.size());
  53. in.close();
  54. GumboOutput* output = gumbo_parse(contents.c_str());
  55. search_for_links(output->root);
  56. gumbo_destroy_output(&kGumboDefaultOptions, output);
  57. }