const axios = require('axios'); const cheerio = require('cheerio'); async function tthashtags(query) { try { if (!query) throw new Error('Query is required'); const { data } = await axios.get(`https://tiktokhashtags.com/hashtag/${query.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()}/`); const $ = cheerio.load(data); const result = { mostPopular: [], secondMostLiked: [], report: {}, top10: [], related: [], trending: [], }; const mostPopularHashtags = $('p1').text(); result.mostPopular = mostPopularHashtags.split(' ').filter(res => res); const secondMostLikedHashtags = $('p2').text(); result.secondMostLiked = secondMostLikedHashtags.split(' ').filter(res => res); result.report = { overallPosts: $('.col-lg-4:nth-child(1) .g-font-size-26').text(), overallViews: $('.col-lg-4:nth-child(2) .g-font-size-26').text(), viewsPerPost: $('.col-lg-4:nth-child(3) .g-font-size-26').text(), }; $('#top10 table tbody tr').each((i, el) => { const hashtag = $(el).find('td:nth-child(2) a').text(); const posts = $(el).find('td:nth-child(3)').text(); const views = $(el).find('td:nth-child(4)').text(); const postViews = $(el).find('td:nth-child(5) .u-label').text(); result.top10.push({ hashtag, posts, views, postViews, }); }); $('#related table tbody tr').each((i, el) => { const hashtag = $(el).find('td:nth-child(2) a').text(); const posts = $(el).find('td:nth-child(3)').text(); const views = $(el).find('td:nth-child(4)').text(); const postViews = $(el).find('td:nth-child(5) .u-label').text(); result.related.push({ hashtag, posts, views, postViews, }); }); $('#tranding table tbody tr').each((i, el) => { const hashtag = $(el).find('td:nth-child(2) a').text(); const posts = $(el).find('td:nth-child(3)').text(); const views = $(el).find('td:nth-child(4)').text(); const postViews = $(el).find('td:nth-child(5) .u-label').text(); result.trending.push({ hashtag, posts, views, postViews, }); }); return result; } catch (error) { throw new Error(error.message); } } // Usage: const resp = await tthashtags('#meme'); console.log(resp);