本文针对 Shopee 平台商品数据抓取中常见的反爬拦截、效率低下、重复请求三大痛点总结了经过生产验证的 3 大核心要点。适用于跨境选品、竞品分析、价格监控等场景一、URL 结构与 ID 快速提取Shopee 全球所有站点采用完全统一的商品 URL 设计无需针对不同地区单独适配解析逻辑这是实现批量自动化抓取的基础。1.全球通用 URL 格式标准商品 URL 结构https://{地区代码}.shopee.com/product/{店铺ID}/{商品ID}常见地区代码tw (中国台湾)、sg (新加坡)、my (马来西亚)、th (泰国)、id (印尼)、ph (菲律宾)、vn (越南)兼容性支持带任意查询参数的 URL如?spmxxxutm_sourceyyy不影响 ID 提取示例importredefget_shopee_ids(url:str)-tuple[str|None,str|None]: 从Shopee商品URL中提取店铺ID和商品ID :param url: Shopee商品完整URL :return: (店铺ID, 商品ID)解析失败返回(None, None) patternr/product/(\d)/(\d)matchre.search(pattern,url)returnmatch.groups()ifmatchelse(None,None)# 测试示例if__name____main__:# http://o0b.cn/alantest_urls[https://shopee.tw/product/123456789/987654321,https://shopee.sg/product/987654321/123456789?spma1z10.1-c-seller,https://invalid-url.com/product/abc/123]forurlintest_urls:shop_id,item_idget_shopee_ids(url)print(fURL:{url})print(f店铺ID:{shop_id},商品ID:二、必带核心请求头Shopee 的反爬系统对请求头校验极为严格只需配置以下 5 个核心请求头即可绕过 90% 以上的基础拦截其余非必要头字段可全部省略减少请求体积基础商品页面请求示例fromcurl_cffiimportrequestsimportrandomimporttime sessionrequests.Session(headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36,Accept:text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language:zh-TW,zh;q0.9,Referer:https://shopee.tw/,Cookie:粘贴你浏览器里的完整 Cookie,},impersonatechrome101,)# http://o0b.cn/alanurls[https://shopee.tw/product/123456789/987654321,https://shopee.tw/product/123456789/123456789,]forurlinurls:rsession.get(url,timeout10)print(url,r.status_code)time.sleep(random.uniform(1.2,2.5))三、ETag 缓存校验Shopee 使用MD5 哈希值作为 ETag 实体标签唯一标识商品页面的特定版本。通过缓存校验机制可避免重复下载未变化的页面内容对于价格、库存等更新频率较低的字段能提升 90% 以上的抓取效率同时大幅降低被平台的风险。ETag 生成规则若因特殊情况无法获取服务器返回的 ETag可使用 Shopee 官方规则自行计算importhashlibdefgenerate_etag(shop_id:str,item_id:str)-str:根据Shopee官方规则生成备用ETagrawf{shop_id}_{item_id}.encode(utf-8)returnf{hashlib.md5(raw).hexdigest()}带缓存的请求代码fromtypingimportDict,Tupleimportrequestsimporthashlib cache:Dict[str,Tuple[str,str]]{}defget_shopee_ids(url:str)-Tuple[str,str]:partsurl.split(/)fori,partinenumerate(parts):ifpartproductandi2len(parts):returnparts[i1],parts[i2]return,defget_common_headers(region:str,cookie:str)-dict:return{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36,Cookie:cookie,Referer:fhttps://{region}.shopee.com/,Accept-Encoding:gzip, deflate, br,Connection:keep-alive,Accept:text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language:zh-TW,zh;q0.9,en;q0.8}defgenerate_etag(shop_id:str,item_id:str)-str:returnhashlib.md5(f{shop_id}_{item_id}.encode()).hexdigest()deffetch_shopee_with_cache(url:str,cookie:str)-str:shop_id,item_idget_shopee_ids(url)ifnotshop_idornotitem_id:raiseValueError(无效的Shopee商品URL)cache_keyf{shop_id}_{item_id}regionurl.split(.)[0].split(//)[-1]headersget_common_headers(region,cookie)ifcache_keyincache:cached_etag,cached_contentcache[cache_key]headers[If-None-Match]cached_etag responserequests.get(url,headersheaders,timeout10)ifresponse.status_code304:print(f[缓存命中]{cache_key}资源未变化使用本地缓存)returncached_contentelifresponse.status_code200:etagresponse.headers.get(ETag,generate_etag(shop_id,item_id))contentresponse.text cache[cache_key](etag,content)print(f[缓存更新]{cache_key}资源已更新缓存已刷新)returncontentelse:response.raise_for_status()if__name____main__:MY_COOKIE替换为你的Shopee Cookie从浏览器复制完整Cookie字符串test_urlhttps://shopee.tw/product/123456789/987654321try:content1fetch_shopee_with_cache(test_url,MY_COOKIE)print(f第一次请求内容长度:{len(content1)}字符\n)content2fetch_shopee_with_cache(test_url,MY_COOKIE)print(f第二次请求内容长度:{len(content2)}字符\n)assertcontent1content2,缓存内容与原始内容不一致print(缓存校验成功两次请求内容完全一致)exceptExceptionase:print(f请求失败:{str(e)})四、避坑指南Cookie 获取与更新从浏览器开发者工具 Network 面板任意 Shopee 页面请求的 Cookie 中复制SPC_EC和SPC_U字段有效期通常为 1-2 周过期后需重新获取请求频率控制建议单 IP 每秒请求不超过 1 次批量抓取时添加random.uniform(0.5, 1.5)随机延迟避免触发 IP 封禁地区一致性请求头中的 Referer 必须与 URL 中的地区代码完全一致否则会被重定向或拦截动态内容处理商品实时价格、库存、销量等数据通过 AJAX 接口加载需单独请求https://{地区}.shopee.com/api/v4/item/get接口获取异常处理添加重试机制使用tenacity库针对 429请求过多、503服务不可用等错误进行指数退避重试
Shopee 商品数据高效抓取:请求与缓存校验 3 大核心
本文针对 Shopee 平台商品数据抓取中常见的反爬拦截、效率低下、重复请求三大痛点总结了经过生产验证的 3 大核心要点。适用于跨境选品、竞品分析、价格监控等场景一、URL 结构与 ID 快速提取Shopee 全球所有站点采用完全统一的商品 URL 设计无需针对不同地区单独适配解析逻辑这是实现批量自动化抓取的基础。1.全球通用 URL 格式标准商品 URL 结构https://{地区代码}.shopee.com/product/{店铺ID}/{商品ID}常见地区代码tw (中国台湾)、sg (新加坡)、my (马来西亚)、th (泰国)、id (印尼)、ph (菲律宾)、vn (越南)兼容性支持带任意查询参数的 URL如?spmxxxutm_sourceyyy不影响 ID 提取示例importredefget_shopee_ids(url:str)-tuple[str|None,str|None]: 从Shopee商品URL中提取店铺ID和商品ID :param url: Shopee商品完整URL :return: (店铺ID, 商品ID)解析失败返回(None, None) patternr/product/(\d)/(\d)matchre.search(pattern,url)returnmatch.groups()ifmatchelse(None,None)# 测试示例if__name____main__:# http://o0b.cn/alantest_urls[https://shopee.tw/product/123456789/987654321,https://shopee.sg/product/987654321/123456789?spma1z10.1-c-seller,https://invalid-url.com/product/abc/123]forurlintest_urls:shop_id,item_idget_shopee_ids(url)print(fURL:{url})print(f店铺ID:{shop_id},商品ID:二、必带核心请求头Shopee 的反爬系统对请求头校验极为严格只需配置以下 5 个核心请求头即可绕过 90% 以上的基础拦截其余非必要头字段可全部省略减少请求体积基础商品页面请求示例fromcurl_cffiimportrequestsimportrandomimporttime sessionrequests.Session(headers{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36,Accept:text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language:zh-TW,zh;q0.9,Referer:https://shopee.tw/,Cookie:粘贴你浏览器里的完整 Cookie,},impersonatechrome101,)# http://o0b.cn/alanurls[https://shopee.tw/product/123456789/987654321,https://shopee.tw/product/123456789/123456789,]forurlinurls:rsession.get(url,timeout10)print(url,r.status_code)time.sleep(random.uniform(1.2,2.5))三、ETag 缓存校验Shopee 使用MD5 哈希值作为 ETag 实体标签唯一标识商品页面的特定版本。通过缓存校验机制可避免重复下载未变化的页面内容对于价格、库存等更新频率较低的字段能提升 90% 以上的抓取效率同时大幅降低被平台的风险。ETag 生成规则若因特殊情况无法获取服务器返回的 ETag可使用 Shopee 官方规则自行计算importhashlibdefgenerate_etag(shop_id:str,item_id:str)-str:根据Shopee官方规则生成备用ETagrawf{shop_id}_{item_id}.encode(utf-8)returnf{hashlib.md5(raw).hexdigest()}带缓存的请求代码fromtypingimportDict,Tupleimportrequestsimporthashlib cache:Dict[str,Tuple[str,str]]{}defget_shopee_ids(url:str)-Tuple[str,str]:partsurl.split(/)fori,partinenumerate(parts):ifpartproductandi2len(parts):returnparts[i1],parts[i2]return,defget_common_headers(region:str,cookie:str)-dict:return{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36,Cookie:cookie,Referer:fhttps://{region}.shopee.com/,Accept-Encoding:gzip, deflate, br,Connection:keep-alive,Accept:text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language:zh-TW,zh;q0.9,en;q0.8}defgenerate_etag(shop_id:str,item_id:str)-str:returnhashlib.md5(f{shop_id}_{item_id}.encode()).hexdigest()deffetch_shopee_with_cache(url:str,cookie:str)-str:shop_id,item_idget_shopee_ids(url)ifnotshop_idornotitem_id:raiseValueError(无效的Shopee商品URL)cache_keyf{shop_id}_{item_id}regionurl.split(.)[0].split(//)[-1]headersget_common_headers(region,cookie)ifcache_keyincache:cached_etag,cached_contentcache[cache_key]headers[If-None-Match]cached_etag responserequests.get(url,headersheaders,timeout10)ifresponse.status_code304:print(f[缓存命中]{cache_key}资源未变化使用本地缓存)returncached_contentelifresponse.status_code200:etagresponse.headers.get(ETag,generate_etag(shop_id,item_id))contentresponse.text cache[cache_key](etag,content)print(f[缓存更新]{cache_key}资源已更新缓存已刷新)returncontentelse:response.raise_for_status()if__name____main__:MY_COOKIE替换为你的Shopee Cookie从浏览器复制完整Cookie字符串test_urlhttps://shopee.tw/product/123456789/987654321try:content1fetch_shopee_with_cache(test_url,MY_COOKIE)print(f第一次请求内容长度:{len(content1)}字符\n)content2fetch_shopee_with_cache(test_url,MY_COOKIE)print(f第二次请求内容长度:{len(content2)}字符\n)assertcontent1content2,缓存内容与原始内容不一致print(缓存校验成功两次请求内容完全一致)exceptExceptionase:print(f请求失败:{str(e)})四、避坑指南Cookie 获取与更新从浏览器开发者工具 Network 面板任意 Shopee 页面请求的 Cookie 中复制SPC_EC和SPC_U字段有效期通常为 1-2 周过期后需重新获取请求频率控制建议单 IP 每秒请求不超过 1 次批量抓取时添加random.uniform(0.5, 1.5)随机延迟避免触发 IP 封禁地区一致性请求头中的 Referer 必须与 URL 中的地区代码完全一致否则会被重定向或拦截动态内容处理商品实时价格、库存、销量等数据通过 AJAX 接口加载需单独请求https://{地区}.shopee.com/api/v4/item/get接口获取异常处理添加重试机制使用tenacity库针对 429请求过多、503服务不可用等错误进行指数退避重试