You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Cheerio提取Instagram高清头像的准确URL?

如何获取Instagram高清头像原图URL(而非320x320缩略图)

我用Cheerio开发Instagram头像提取工具,现在遇到个问题——代码只能拿到320x320分辨率的头像URL,但Chrome开发者工具里能看到并获取高清原图的URL。

当前获取的320x320头像URL示例:

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

实际高清头像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

打开两个链接就能看到分辨率差异。以下是我的代码:

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; 

解决方案

问题根源

你当前的URL处理逻辑只针对旧格式的_s320x320路径后缀,但现在Instagram用CDN的stp查询参数控制图片尺寸,比如stp=dst-jpg_s320x320_tt6就是指定320x320缩略图的参数。另外,profile_pic_url_hd返回的链接可能已经被默认添加了尺寸限制参数。

具体修复步骤

  1. 优先获取API中的原图链接字段
    Instagram的API返回数据里,user对象下的hd_profile_pic_url_info.url是直接指向高清原图的链接,优先级要高于profile_pic_url_hd。

  2. 移除CDN的尺寸控制参数
    用URL对象解析链接,删除stp参数即可让CDN返回原图。

  3. 更新请求头的User-Agent
    旧版本的User-Agent可能会触发Instagram返回旧格式或受限的链接,换成最新的浏览器UA能获取更完整的API数据。

修改后的核心代码片段

替换你代码中处理头像URL的部分:

if (response.data && response.data.data && response.data.data.user) {
    // 优先获取hd_profile_pic_url_info中的原图链接
    const profilePicUrl = response.data.data.user.hd_profile_pic_url_info?.url 
                        || response.data.data.user.profile_pic_url_hd 
                        || response.data.data.user.profile_pic_url;
    
    // 解析URL并移除尺寸控制参数
    const url = new URL(profilePicUrl);
    url.searchParams.delete('stp');
    const fullSizePicUrl = url.toString();

    console.log('Found profile picture URL:', fullSizePicUrl);
    res.json({ imageUrl: fullSizePicUrl });
}

同时更新请求头的User-Agent:

headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Accept': 'application/json',
    'X-IG-App-ID': '936619743392459',
    'X-Requested-With': 'XMLHttpRequest',
}

验证效果

修改后生成的链接会和你在开发者工具中看到的高清原图URL一致,打开后就能获取最大分辨率的头像。

内容的提问来源于stack exchange,提问作者Ashwin Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 09:27:03