Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Cheerio returns empty string while scraping target.com

I am trying to scrape a product page of target.com to retrieve the product title, price and image using cheerio. Using the code below I was able to retrieve the title and image, but the price always returns an empty string. I am new to scraping and I have run out of my wits. Any help is appreciated.

Thank you!

const axios = require('axios')
const cheerio = require('cheerio');
const { html } = require('cheerio/lib/api/manipulation');
const express = require('express')
const { get } = require('http')

async function getTargetProductDetails(prodUrl) {
    try {
        const scrapeUrl = prodUrl

        const { data } = await axios({
            method: "GET",
            url: scrapeUrl,
            headers: {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36",
            },
        })
        const $ = cheerio.load(data)
        const prodTitleElemSelector = '#pageBodyContainer > div:nth-child(2) > div.h-padding-h-default > h1 > span'
            
        const prodPriceElemSelector = '#pageBodyContainer > div:nth-child(2) > div.styles__StyledRow-sc-1nuqtm0-0.znNOo > div.styles__StyledCol-sc-ct8kx6-0.CBMCR.styles__StyledSecondCol-sc-y0ahq4-2.jftnqo.h-padding-h-default > div > div.h-margin-b-x2 > div:nth-child(1) > span'
        const imgElemSelector = '#pageBodyContainer > div:nth-child(2) > div.styles__StyledRow-sc-1nuqtm0-0.znNOo > div.styles__StyledCol-sc-ct8kx6-0.cQDVpz.styles__StyledFirstCol-sc-2vujr8-3.hHPFGX.h-padding-h-default > div:nth-child(1) > section > div.styles__CarouselProductFilmstripWrapper-sc-cwwbs3-2.fMvUGZ > div.styles__NicolletFilmstripDiv-sc-1ldzhqm-0.ctFgTt.BaseCarousel-sc-pnaqvl-0.loHErH > div > button:nth-child(1) > div > div > div > div'

        console.log($(prodTitleElemSelector).text().trim())
        console.log($(imgElemSelector).find('img').attr('src'))
        console.log($(prodPriceElemSelector).text())
        


    } catch (err) {
        console.error(err)
    }

}

getTargetProductDetails('https://www.target.com/p/nintendo-switch-lite/-/A-83031385?preselect=77419248#lnk=sametab')

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The data was actually being fetched via few APIs. Inspecting the page source revealed the json objects. Extracting the json data was all I had to do to get what I wanted.

Here is the exact solution !

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!