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

使用urllib3无法获取图片问题排查求助

使用urllib3无法获取目标图片排查

我想知道自己忽略了什么——使用urllib3发起请求无法获取目标图片,相关代码如下:

import urllib3

url = "https://www.mouser.com/images/adi/sm/ITP_ADI_SOT-23-6_05-08-1636_t.jpg"

http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)

已通过调试逐步排查,调试截图:
Debug snap

常见问题及解决办法

  • 缺少合法的User-Agent请求头:Mouser这类电商网站会校验请求的User-Agent字段,urllib3默认请求不带该字段或标识过于简单,会被服务器判定为非浏览器请求直接拦截(通常返回403状态码)。
  • 修正后的代码:
import urllib3

url = "https://www.mouser.com/images/adi/sm/ITP_ADI_SOT-23-6_05-08-1636_t.jpg"

http = urllib3.PoolManager()
# 添加模拟主流浏览器的User-Agent
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
}
response = http.request('GET', url, headers=headers)
print(response.status)

# 状态码为200时保存图片
if response.status == 200:
    with open('target_image.jpg', 'wb') as file:
        file.write(response.data)
  • 若添加User-Agent后仍无法获取,可尝试补充Referer字段(设置为https://www.mouser.com/),部分网站会校验请求来源。

内容的提问来源于stack exchange,提问作者86Meesta2

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:45:40