I m making a insta profile image extracter using cheerio now the problem is it is not fetching the exact size of a image like when i inspected a image page on insta in the chrome developer tools i was able to see full and hd image in a new tab now in my code i my code it is just fetching the 320×320 image here is the example of image url that my code fetch’s.
https://instagram.fkhi25-1.fna.fbcdn.net/v/t51.2885-19/437937765_2168167833531480_2736325614077431115_n.jpg?stp=dst-jpg_s320x320_tt6&_nc_ht=instagram.fkhi25-1.fna.fbcdn.net&_nc_cat=1&_nc_oc=Q6cZ2AHo6DXdTchbD3TRojEikoLin3hj-b8oxc_jMqzHut5s3KusCglE2AxiS0ZSFDdNx5Q&_nc_ohc=QONOOjHPgZ4Q7kNvgGD_IME&_nc_gid=efc7a41b006a4b03b7f8317f333ad110&edm=AOQ1c0wBAAAA&ccb=7-5&oh=00_AYDr_14nnokGjuhvgNMDdakPa6lbRc53lreWp_SeztiLww&oe=67739EAE&_nc_sid=8b3546
And here is the actual image url.
https://instagram.fkhi25-1.fna.fbcdn.net/v/t51.2885-19/437937765_2168167833531480_2736325614077431115_n.jpg?_nc_ht=instagram.fkhi25-1.fna.fbcdn.net&_nc_cat=1&_nc_oc=Q6cZ2AHEtqqQYzAKMOwbn3lvbGDq4e3kKNxeS347vWwQbL1ZY9sZvSeG9wRBc5tjEOJDC0A&_nc_ohc=QONOOjHPgZ4Q7kNvgE7pwyH&_nc_gid=a5163103d3184dcd86d160c27039f1c3&edm=APoiHPcBAAAA&ccb=7-5&oh=00_AYDCzN-s9DKjtt-08LZKqTOOTToI2ODjAgt7p16FRDf7aw&oe=67739EAE&_nc_sid=22de04
You will know the difference when you open both of them.
Sorry for the bad code i m a bit begineer.
const express = require('express');
const router = express.Router();
const app = express();
const axios = require('axios');
const path = require('path');
const fs = require('fs');
const cheerio = require('cheerio');
const { InstaClient } = require('insta-fetcher');
require('dotenv').config();
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
// Define the route for rendering mp3.ejs
router.get('/mp3', (req, res) => {
res.render('mp3');
});
app.use(express.urlencoded({ extended: true }));
const puppeteer = require("puppeteer");
// Connect to Instagram
// API endpoint to fetch profile picture
// API endpoint to fetch profile picture using Puppeteer
const bodyParser = require('body-parser');
app.use(bodyParser.json());
function extractUsername(url) {
const matches = url.match(/instagram.com/([A-Za-z0-9_.]+)/);
return matches ? matches[1] : null;
}
router.post('/api/get-profile-pic', async (req, res) => {
try {
const { profileUrl } = req.body;
console.log('Received request for:', profileUrl);
if (!profileUrl) {
return res.status(400).json({ error: 'Profile URL is required' });
}
// Extract username from URL
const username = extractUsername(profileUrl);
if (!username) {
return res.status(400).json({ error: 'Invalid Instagram URL' });
}
// Use Instagram's public API
const response = await axios.get(`https://www.instagram.com/api/v1/users/web_profile_info/?username=${username}`, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'application/json',
'X-IG-App-ID': '936619743392459',
'X-Requested-With': 'XMLHttpRequest',
}
});
if (response.data && response.data.data && response.data.data.user) {
// Attempt to get the HD profile picture
const profilePicUrl = response.data.data.user.profile_pic_url_hd || response.data.data.user.profile_pic_url;
// Optionally, you can remove the size portion of the URL to ensure it's the full-size picture
const fullSizePicUrl = profilePicUrl.replace(/_s[0-9]+x[0-9]+/, ''); // Removes the _s320x320 part from the URL if it exists
console.log('Found profile picture URL:', fullSizePicUrl);
res.json({ imageUrl: fullSizePicUrl });
} else {
throw new Error('Profile not found');
}
} catch (error) {
console.error('Error details:', error);
res.status(500).json({
error: 'Failed to fetch profile picture',
details: error.message,
});
}
});
module.exports = router;
if some one can help me through this problem that would be appreciated.
Ashwin Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1