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

Laravel/Python中无法从Shein ShareJump链接爬取商品价格的技术求助

解决方案:从Shein ShareJump链接提取商品关键信息

核心思路

先解析ShareJump链接获取商品ID,再调用Shein的移动端商品详情API(反爬限制更宽松),无需模拟浏览器,可直接在Laravel/Python环境运行,且避免CAPTCHA触发。


步骤1:解析ShareJump链接获取商品ID

请求ShareJump链接会返回包含目标商品页URL的JSON,从中提取商品ID(格式如123456),同时保留链接中的localcountry参数用于后续地区化请求。

步骤2:调用Shein移动端商品详情API

使用模拟移动端的请求头调用商品详情API,获取包含价格、原价、折扣、库存等关键信息的响应。


Python实现示例

import requests
import re

def extract_shein_product_info(sharejump_url):
    # 模拟移动端请求头,降低反爬触发概率
    headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'en-US,en;q=0.9'
    }

    # 1. 请求ShareJump链接,获取商品页URL
    try:
        jump_resp = requests.get(sharejump_url, headers=headers, timeout=10)
        jump_resp.raise_for_status()
        jump_data = jump_resp.json()
    except Exception as e:
        print(f"请求ShareJump链接失败: {str(e)}")
        return None

    product_url = jump_data.get('targetUrl')
    if not product_url:
        print("未获取到目标商品页URL")
        return None

    # 提取商品ID(从商品页URL中匹配-p-后的数字)
    match = re.search(r'-p-(\d+)-', product_url)
    if not match:
        print("无法从商品页URL中提取商品ID")
        return None
    product_id = match.group(1)

    # 获取localcountry参数
    local_country = sharejump_url.split('localcountry=')[-1]

    # 2. 请求移动端商品详情API
    api_url = f'https://api-shein.com/h5/product/detail/1/get_detail?product_id={product_id}&local_country={local_country}'
    try:
        product_resp = requests.get(api_url, headers=headers, timeout=10)
        product_resp.raise_for_status()
        product_data = product_resp.json()
    except Exception as e:
        print(f"请求商品详情API失败: {str(e)}")
        return None

    # 提取关键信息
    if product_data.get('code') == 200:
        detail = product_data['data']['detail']
        return {
            'sale_price': detail.get('salePrice'),
            'original_price': detail.get('origPrice'),
            'discount': detail.get('discount'),
            'currency': detail.get('currency'),
            'stock': detail.get('stock'),
            'product_name': detail.get('name')
        }
    else:
        print(f"API返回错误: {product_data.get('msg')}")
        return None

# 使用示例
sharejump_link = "http://api-shein.shein.com/h5/sharejump/appjump?link=l5RjILzGnr1_8&localcountry=AE"
product_info = extract_shein_product_info(sharejump_link)
if product_info:
    print(product_info)

Laravel(GuzzleHttp)实现示例

<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

function getSheinProductDetails(string $sharejumpUrl): ?array
{
    $client = new Client([
        'timeout' => 10,
        'headers' => [
            'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1',
            'Accept' => 'application/json, text/plain, */*',
            'Accept-Language' => 'en-US,en;q=0.9'
        ]
    ]);

    // 1. 解析ShareJump链接获取商品ID
    try {
        $jumpResponse = $client->get($sharejumpUrl);
        $jumpData = json_decode($jumpResponse->getBody()->getContents(), true);
    } catch (RequestException $e) {
        \Log::error('请求ShareJump链接失败: ' . $e->getMessage());
        return null;
    }

    if (!isset($jumpData['targetUrl'])) {
        \Log::error('未获取到目标商品页URL');
        return null;
    }

    preg_match('/-p-(\d+)-/', $jumpData['targetUrl'], $matches);
    if (empty($matches)) {
        \Log::error('无法提取商品ID');
        return null;
    }
    $productId = $matches[1];
    $localCountry = explode('localcountry=', $sharejumpUrl)[1];

    // 2. 请求商品详情API
    $apiUrl = "https://api-shein.com/h5/product/detail/1/get_detail?product_id={$productId}&local_country={$localCountry}";
    try {
        $productResponse = $client->get($apiUrl);
        $productData = json_decode($productResponse->getBody()->getContents(), true);
    } catch (RequestException $e) {
        \Log::error('请求商品详情API失败: ' . $e->getMessage());
        return null;
    }

    if ($productData['code'] !== 200) {
        \Log::error('API返回错误: ' . $productData['msg']);
        return null;
    }

    $detail = $productData['data']['detail'];
    return [
        'sale_price' => $detail['salePrice'] ?? null,
        'original_price' => $detail['origPrice'] ?? null,
        'discount' => $detail['discount'] ?? null,
        'currency' => $detail['currency'] ?? null,
        'stock' => $detail['stock'] ?? null,
        'product_name' => $detail['name'] ?? null
    ];
}

// 使用示例
$sharejumpLink = "http://api-shein.shein.com/h5/sharejump/appjump?link=l5RjILzGnr1_8&localcountry=AE";
$productInfo = getSheinProductDetails($sharejumpLink);
dd($productInfo);
?>

反爬规避注意事项

  • 请求头模拟:始终使用移动端User-Agent,避免PC端的严格CAPTCHA验证;定期轮换User-Agent池中的标识
  • 请求频率控制:单IP请求间隔不低于2秒,高请求量场景需搭配代理池轮换IP
  • API端点更新:Shein的API可能随时调整,若当前API失效,可通过抓包Shein移动端APP的商品详情请求获取新端点

内容的提问来源于stack exchange,提问作者Mahmod Algeriany

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 06:44:51