'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')
Solution 1:[1]
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 !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | roger |
