How to extract certain information from return type using apify API and Node.js

137 Views Asked by At

I am trying to get gas prices for a web app I am making. I found a web scraper from Apify that gets the data I need. The problem is I'm not sure how to extract just the gas prices from the returned object from the API. There is a lot of information.

Here is the code I run in node:

import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with API token
const client = new ApifyClient({
    token: 'apify_api_********',
});

// Prepare actor input
const input = {
    "location": "Indiana",
    "maxCrawledPlacesPerSearch": 1
};

(async () => {
    // Run the actor and wait for it to finish
    const run = await client.actor("natasha.lekh/gas-prices-scraper").call(input);

    // Fetch and print actor results from the run's dataset (if any)
    console.log('Results from dataset');
    const { items } = await client.dataset(run.defaultDatasetId).listItems();
    items.forEach((item) => {
        console.log(item);

})();

Which returns:

Results from dataset
{
  title: 'Luke',
  subTitle: null,
  description: null,
  price: null,
  menu: null,
  categoryName: 'Gas station',
  address: '1051 Indianapolis Blvd, Hammond, IN 46320',
  locatedIn: null,
  neighborhood: '1051 Indianapolis Blvd',
  street: '1051 Indianapolis Blvd',
  city: 'Hammond',
  postalCode: '46320',
  state: 'Indiana',
  countryCode: 'US',
  plusCode: 'MFVP+R3 Hammond, Indiana',
  website: 'http://uluke.com/',
  phone: '(219) 473-1425',
  temporarilyClosed: false,
  claimThisBusiness: false,
  location: { lat: 41.6945038, lng: -87.5147858 },
  permanentlyClosed: false,
  totalScore: 3.7,
  isAdvertisement: false,
  rank: 1,
  placeId: 'ChIJm_BHBfbYEYgRZee8gFZeG7o',
  categories: [ 'Gas station', 'Convenience store' ],
  cid: '13410416041045845861',
  url: 'https://www.google.com/maps/place/Luke/@41.6945038,-87.5147858,17z/data=!3m1!4b1!4m6!3m5!1s0x8811d8f60547f09b:0xba1b5e5680bce765!8m2!3d41.6945038!4d-87.5147858!16s%2Fg%2F1ptvtxy1r?hl=en',
  searchPageUrl: 'https://www.google.com/maps/search/gas%20station%20in%20Indiana?hl=en',
  searchPageLoadedUrl: 'https://www.google.com/maps/search/gas+station+in+Indiana/@40.7310665,-86.5930181,8z?hl=en',
  searchString: 'gas station in Indiana',
  scrapedAt: '2023-02-11T04:17:35.902Z',
  popularTimesLiveText: null,
  popularTimesLivePercent: null,
  popularTimesHistogram: {
    Su: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    Mo: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    Tu: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    We: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    Th: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    Fr: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ],
    Sa: [
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object],
      [Object], [Object], [Object]
    ]
  },
  openingHours: [
    { day: 'Monday', hours: '5 AM to 10 PM' },
    { day: 'Tuesday', hours: '5 AM to 10 PM' },
    { day: 'Wednesday', hours: '5 AM to 10 PM' },
    { day: 'Thursday', hours: '5 AM to 10 PM' },
    { day: 'Friday', hours: '5 AM to 10 PM' },
    { day: 'Saturday', hours: '5 AM to 10 PM' },
    { day: 'Sunday', hours: '5 AM to 10 PM' }
  ],
  peopleAlsoSearch: [
    {
      category: 'People also search for',
      title: 'BP Luke Gas Station',
      reviewsCount: 441,
      totalScore: 3.3
    },
    {
      category: 'People also search for',
      title: 'Luke Gas Station Car Wash',
      reviewsCount: 9,
      totalScore: 2.2
    },
    {
      category: 'People also search for',
      title: 'GoLo',
      reviewsCount: 331,
      totalScore: 3.8
    },
    {
      category: 'People also search for',
      title: 'Shell',
      reviewsCount: 104,
      totalScore: 3.5
    },
    {
      category: 'People also search for',
      title: "Luke's Gas Station",
      reviewsCount: 0,
      totalScore: 0
    }
  ],
  additionalInfo: {
    Accessibility: [ [Object], [Object] ],
    Offerings: [ [Object] ],
    Amenities: [ [Object] ],
    Payments: [ [Object] ]
  },
  reviewsCount: 283,
  reviewsDistribution: {
    oneStar: 41,
    twoStar: 10,
    threeStar: 51,
    fourStar: 67,
    fiveStar: 114
  },
  imagesCount: 8,
  reviews: [],
  reviewsTags: [
    { title: 'prices', count: 21 },
    { title: 'pumps', count: 15 },
    { title: 'clean', count: 8 },
    { title: 'pay', count: 6 },
    { title: 'credit cards', count: 5 },
    { title: 'convenient', count: 3 }
  ],
  orderBy: [],
  gasPrices: [
    {
      priceTag: '$3.30',
      updatedAt: '2023-02-10T14:46:30.000Z',
      unit: 'gallon',
      currency: 'USD',
      price: 3.3,
      gasType: 'Regular'
    },
    {
      priceTag: '$3.70',
      updatedAt: '2023-02-10T05:37:40.000Z',
      unit: 'gallon',
      currency: 'USD',
      price: 3.7,
      gasType: 'Midgrade'
    },
    {
      priceTag: '$4.10',
      updatedAt: '2023-02-10T05:37:40.000Z',
      unit: 'gallon',
      currency: 'USD',
      price: 4.1,
      gasType: 'Premium'
    },
    {
      priceTag: '$5.00',
      updatedAt: '2023-02-10T06:52:21.000Z',
      unit: 'gallon',
      currency: 'USD',
      price: 5,
      gasType: 'Diesel'
    }
  ]
}

All I want is the last bit, the priceTag and gasType for each type of gas. How would I get that data out of the returned object?

I thought it was a JSON file so I tried a few things I found online, but I'm not really sure where to start.

0

There are 0 best solutions below