Sadia Mubeen
Sadia Mubeen

Reputation: 21

How to use Axios with Google Apps Script

I'm trying to perform web scrapping through web scrapping API's. For using the API, it's mandatory to some extent to use axios. Now I'm unable to find a way to use axios with web editor.

I tried the given method and the code always returns null.

  1. Go to the Axios repository on GitHub at https://github.com/axios/axios.
  2. Click on the "releases" tab and download the latest release of the library as a zip file.
  3. Extract the zip file to a folder on your computer.
  4. In the Google Apps Script editor, create a new script file by clicking on the "+" icon next to "Files" in the left sidebar.
  5. Name the file "axios.gs".
  6. Copy the contents of the "dist/axios.min.js" file from the folder you extracted in step 3 and paste it into the "axios.gs" file in the Google Apps Script editor.
  7. Save the "axios.gs" file.
const query = 'site:linkedin.com/company "' + 'Company Name' + '"';

  const options = {
    token: "223E9D905C2BC87EBD5181677A8C240A",
    url: 'https://www.google.com/search?q=' + encodeURIComponent(query)
  };

  axios.get("https://scraperbox.com/api/scrape", options)
    .then(response => {
      Logger.log(response.data)
    }).catch(error => {
      Logger.log(error.response.data.errors || error)
    });

Upvotes: 2

Views: 2740

Answers (1)

Artem P
Artem P

Reputation: 5332

I also tried to bypass UrlFetchApp.fetch with axios. I changed your code to:

async function fetchtest(refresh) {
  const url = `https://randomuser.me/api/`
  await axios.get(url)
    .then(response => {
      Logger.log(response)
    }).catch(error => {
      Logger.log(error)
    });
}

And it works but I got error:

There is no suitable adapter to dispatch the request since :
- adapter xhr is not supported by the environment
- adapter http is not available in the build

So XMLHttpRequest is unavailable, fetch also unavailable. http from Node also unavailable.

There is nothing else available from ES6, so we forced to use UrlFetchApp.fetch.

Upvotes: 0

Related Questions