diff --git a/Web/ProxyImagesDemo/demo.py b/Web/ProxyImagesDemo/demo.py new file mode 100644 index 0000000..5396777 --- /dev/null +++ b/Web/ProxyImagesDemo/demo.py @@ -0,0 +1,31 @@ +@app.route('/proxy-image/') +def proxy_image(image_url): + """ + 图片代理路由,用于绕过跨域限制 + """ + try: + # 重新构建完整URL(如果需要的话) + if not image_url.startswith(('http://', 'https://')): + full_url = 'https://' + image_url.lstrip('/') + else: + full_url = image_url + + # 添加请求头来模拟浏览器请求 + 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', + 'Referer': 'https://movie.douban.com/', + 'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8' + } + + response = requests.get(full_url, headers=headers, timeout=10) + + if response.status_code == 200: + # 返回图片内容 + image_data = BytesIO(response.content) + content_type = response.headers.get('Content-Type', 'image/jpeg') + return send_file(image_data, mimetype=content_type) + else: + abort(404) + except Exception as e: + print(f"Error fetching image: {e}") + abort(404) \ No newline at end of file