I want to grab the username of each seller on this poshmark webpage using javascript, cheerio, and axiom

74 Views Asked by At

I've tried a lot of different things, and this seems to be the closest, but it only returns one of the div items i looking for...

I've gotten it to return multiple, BUT whenever it does return multiple divs - It returns blank list when i try to call the .text() function on the html and enter it into list (I've since deleted that code.)

Here is the webpage, and if you check every single item box has a seller name. I've tried for about 5 hours now, and im obviously a beginner especially in JS so it's been a good challenge. I think im lacking some fundamental knowledge which is holding me back.

https://poshmark.com/category/Men-Jackets_&_Coats?sort_by=like_count&all_size=true&my_size=false

Thank you to everyone who offers help. Happy holidays.

const express = require("express");
const cheerio = require("cheerio");
const request = require("request-promise");
const pretty = require("pretty");
const { default: axios } = require("axios");
const app = express();
const port = process.env.port || 5000;

let states =[];

const url = "https://poshmark.com/category/Men-Jackets_&_Coats?sort_by=like_count&all_size=true&my_size=false";

const fetchData = async () => {

    try {
        let res = await axios.get(url);
        let $ = await cheerio.load(res.data);

        const items = $("#content > div > div > div > div:nth-child(4) > section > div.tiles_container.m--t--1");      

        const itemArea = $("#content > div > div > div > div:nth-child(4) > section > div.tiles_container.m--t--1 > div:nth-child(1) > div > div")

        itemArea.each(function(i){
            itemHref = itemArea.find("href").text()
            areaText = itemArea.text();
            console.log(areaText);

            console.log(itemHref);

            //console.log(`${i} : ${itemArea}\n\n\n`)
        });
        
    } catch (error) {
        console.log(error)
        return
    }
};

fetchData();

I tried grabbing the href and grabbing the class with the username at the bottom of each div, and each time it returned blank or undefined once i thought i finally got it.

1

There are 1 best solutions below

1
On

It may just be an issue with your selector. however, I found that this works:

const itemArea = $(".tiles_container a.tile__creator span");

itemArea.each(function (i, element) {
    console.log('username: ', $(element).text());
});

this way we get only the user name in the text and not the entire card text.