{"id":1765,"date":"2013-08-15T11:58:13","date_gmt":"2013-08-15T11:58:13","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1765"},"modified":"2013-08-15T11:58:13","modified_gmt":"2013-08-15T11:58:13","slug":"scraping-a-list-of-links-from-a-document-into-a-csv-file","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scraping-a-list-of-links-from-a-document-into-a-csv-file\/","title":{"rendered":"Scraping a list of links from a document into a CSV file"},"content":{"rendered":"<p>First, right click an element you are interested in, select &#8220;Inspect Element&#8221;. In the Developer Tools window, select &#8220;Copy XPath&#8221;. If all goes well, this will be an array valued path, and you can modify it slightly to return all nodes, instead of the selected item.<\/p>\n<pre lang=\"javascript\">\nnodes = document.evaluate( '\/\/*[@id=\"hm-lower-background\"]\/div\/a' ,document, \n   null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null )\n\nfunction csvEscape(value) {\n  return '\"' + value.replace(\/\"\/g, '\\\\\"') + '\"'\n}\n\nfor (var i = 0; i < nodes.snapshotLength; i++) { \n  var item = nodes.snapshotItem(i);\n  if (item.innerText != \"\") {\n    console.log(csvEscape(item.innerText) + \",\" + csvEscape(item.href)); \n  }\n}\n<\/pre>\n<p>Which gives you output like this:<\/p>\n<pre>\n\"Cathedral Basilica of SS Peter&Paul, Philadelphia PHILA. SOUTH\",\"http:\/\/archphila.org\/parishes\/7000.php\"\n\"St. Agatha , Philadelphia \",\"http:\/\/archphila.org\/parishes\/0.php\"\n\"St. Adalbert (Polish) , Philadelphia PHILADELPHIA-NORTH\",\"http:\/\/archphila.org\/parishes\/7485.php\"\n\"St. Agatha-St. James , Philadelphia PHILADELPHIA-SOUTH\",\"http:\/\/archphila.org\/parishes\/7490.php\"\n\"St. Agnes , Sellersville BUCKS COUNTY\",\"http:\/\/archphila.org\/parishes\/7500.php\"\n\"St. Agnes , West Chester CHESTER COUNTY\",\"http:\/\/archphila.org\/parishes\/7505.php\"\n\"St. Agnes-St. John Nepomucene (Slovak) , Philadelphia PHILADELPHIA-SOUTH\",\"http:\/\/archphila.org\/parishes\/7495.php\"\n\"St. Albert the Great , Huntingdon Valley MONTGOMERY COUNTY\",\"http:\/\/archphila.org\/parishes\/7510.php\"\n\"St. Alice , Upper Darby DELAWARE COUNTY\",\"http:\/\/archphila.org\/parishes\/7515.php\"\n\"All Saints , Philadelphia PHILADELPHIA-NORTH\",\"http:\/\/archphila.org\/parishes\/7010.php\"\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First, right click an element you are interested in, select &#8220;Inspect Element&#8221;. In the Developer Tools window, select &#8220;Copy XPath&#8221;. If all goes well, this will be an array valued path, and you can modify it slightly to return all nodes, instead of the selected item. nodes = document.evaluate( &#8216;\/\/*[@id=&#8221;hm-lower-background&#8221;]\/div\/a&#8217; ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scraping-a-list-of-links-from-a-document-into-a-csv-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scraping a list of links from a document into a CSV file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[495],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1765"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1765"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1765\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}