简体   繁体   中英

What is the JavaScript equivalent of ImportXML?

What is the JavaScript equivalent of ImportXML ?

Have a Google spreadsheet of 10,000 rows that each contain a city and state. To find their zip code, neither the ImportXML formula nor LibreOffice's FilterXML have worked .

Ok so to answer you question I have both includes a simple apps script on getting the piece of data you want and also a simpler importxml call that does work with the option to return only 1 or all of them:

for importxml it says 50 calls but honestly I used to use importxml for everything and you can load much more than that, they just load as the data returns, so not in order but you can do more than 50 easily

to simplify your import xml first here is a modification to the construction of the url string:

=SUBSTITUTE("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&city="&E2&"&state="&F2," ","+")

You can still use a %20 if you want instead of the + but i switched it only because that is what the usps site defaults to, i tested both though and both work.

Here I was using instead of concatenate you can you & which effectively does the same thing but is easier for readability. Then you can wrap the whole thing with your substitute to catch any and all spaces in your concatenated data.

Ill add an image below where you can see the output in this order:

To pull in the list as a semicolon delimited in one cell:

=JOIN(";",IMPORTXML(SUBSTITUTE("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&city="&E2&"&state="&F2," ","+"),"//*[@class='zip']"))

To get only the first zip code listed:

=SPLIT(IMPORTXML(SUBSTITUTE("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&city="&E2&"&state="&F2," ","+"),"//*[@class='zip']/text()")," ")

For the ability to pull in all the related zip codes (as a list):

=IMPORTXML(SUBSTITUTE("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&city="&E2&"&state="&F2," ","+"),"//*[@class='zip']")

and finally if you really want to still use this as an app script, here is a simple method:

  function zipcodes(url) {
  var found, html, content = '';
  var response = UrlFetchApp.fetch(url);
  if (response) {
    html = response.getContentText();
    if (html) content = html.match(/<span class="zip" style="">(\d+)<\/span><span class="zip/gi)[0].match(/zip" style="">(\d+)<\/span>/i)[1];
  }
  return content;
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM