mirror of
https://gitee.com/insArvin/nypc_python_advanced.git
synced 2026-04-18 03:32:29 +08:00
Compare commits
10 Commits
b4aacf97a9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4556bbf2c7 | ||
|
|
0501531afd | ||
|
|
2144a693db | ||
|
|
03ba00e127 | ||
|
|
8373d19993 | ||
|
|
dfff508448 | ||
|
|
b81065c9ba | ||
|
|
c5842ac19a | ||
|
|
2ce1f13d8e | ||
|
|
3c4315c40f |
@@ -42,6 +42,8 @@
|
||||
|
||||
:calendar: 2025-12-18:提交案例代码“[豆瓣电影表格展示](./Web/DoubanMovieWeb)”
|
||||
|
||||
:calendar: 2026-01-03:提交DEMO代码“[图片代理接口](./Web/ProxyImagesDemo)”
|
||||
|
||||
【:warning: 本仓库代码保持更新......】
|
||||
|
||||
#### 关于课程视频
|
||||
|
||||
BIN
Spider/ecoDataServer/pics/image-20260106195132490.png
Normal file
BIN
Spider/ecoDataServer/pics/image-20260106195132490.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 109 KiB |
BIN
Spider/ecoDataServer/pics/image-20260106195505299.png
Normal file
BIN
Spider/ecoDataServer/pics/image-20260106195505299.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 360 KiB |
151
Spider/ecoDataServer/readme.md
Normal file
151
Spider/ecoDataServer/readme.md
Normal file
@@ -0,0 +1,151 @@
|
||||
# EcoData 数据接口文档
|
||||
|
||||
## 项目概述
|
||||
|
||||
EcoData 是一个基于数据查询系统,提供数据查询。
|
||||
|
||||
数据为测试生成数据,仅供测试。
|
||||
|
||||
## 基本URL
|
||||
|
||||
`http://ecodata.lzgxy.cn`
|
||||
|
||||
## 接口列表
|
||||
|
||||
### 1. 分页数据查询接口
|
||||
|
||||
- **接口地址**:`/query/PaginQuery/`
|
||||
- **请求方式**:GET
|
||||
- **认证方式**:密钥验证
|
||||
|
||||
#### 请求参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 |
|
||||
| ---------- | ------- | ---- | ------------------- |
|
||||
| key | String | `是` | 访问密钥 |
|
||||
| user_id | String | 否 | 用户ID |
|
||||
| product_id | String | 否 | 产品ID |
|
||||
| start_time | String | 否 | 开始时间(ISO格式) |
|
||||
| end_time | String | 否 | 结束时间(ISO格式) |
|
||||
| city | String | 否 | 用户所在城市 |
|
||||
| gender | String | 否 | 用户性别 |
|
||||
| age | Integer | 否 | 用户年龄 |
|
||||
| platform | String | 否 | 购物平台 |
|
||||
| offset | Integer | 否 | 起始索引,默认为0 |
|
||||
| limit | Integer | 否 | 返回数量,默认为10 |
|
||||
|
||||
#### 请求头
|
||||
|
||||
- 需要在URL参数中包含 `key` 参数,用于身份验证
|
||||
|
||||
#### 响应示例
|
||||
|
||||
```json
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"id": 1,
|
||||
"user_id": "123456",
|
||||
"order_id": "123456000001",
|
||||
"user_name": "张三",
|
||||
"product_id": "P001",
|
||||
"product_name": "商品名称",
|
||||
"product_category": "商品分类",
|
||||
"unit_price": 99.99,
|
||||
"purchase_time": "2023-01-01T10:00:00",
|
||||
"quantity": 2,
|
||||
"total_amount": 199.98,
|
||||
"user_city": "北京",
|
||||
"user_gender": "男",
|
||||
"user_age": 25,
|
||||
"shopping_platform": "淘宝"
|
||||
}
|
||||
],
|
||||
"pagination": {
|
||||
"offset": 0,
|
||||
"limit": 10,
|
||||
"total": 100,
|
||||
"has_more": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 浏览器示例
|
||||
|
||||

|
||||
|
||||
#### 响应参数说明
|
||||
|
||||
- `data`:数据列表
|
||||
- id:记录ID
|
||||
- user_id:用户ID
|
||||
- order_id:订单ID
|
||||
- user_name:用户名
|
||||
- product_id:产品ID
|
||||
- product_name:产品名称
|
||||
- product_category:产品分类
|
||||
- unit_price:单价
|
||||
- purchase_time:购买时间
|
||||
- quantity:数量
|
||||
- total_amount:总金额
|
||||
- user_city:用户城市
|
||||
- user_gender:用户性别
|
||||
- user_age:用户年龄
|
||||
- shopping_platform:购物平台
|
||||
- `pagination`:分页信息
|
||||
- offset:起始索引
|
||||
- limit:返回数量
|
||||
- total:总记录数
|
||||
- has_more:是否有更多数据
|
||||
|
||||
### 2. 密钥生成接口
|
||||
|
||||
- **接口地址**:`/get_signature/`
|
||||
- **请求方式**:GET
|
||||
|
||||
#### 请求参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 |
|
||||
| ------ | ------ | ---- | -------- |
|
||||
| id | String | 是 | 学生学号 |
|
||||
| name | String | 是 | 学生姓名 |
|
||||
|
||||
#### 响应示例
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "张三",
|
||||
"student_id": "20210001",
|
||||
"ip": "192.168.1.1",
|
||||
"request_key": "XXXXXXXXX(访问密钥)"
|
||||
}
|
||||
```
|
||||
|
||||
#### 浏览器示例
|
||||
|
||||

|
||||
|
||||
#### 响应参数说明
|
||||
|
||||
- name:学生姓名
|
||||
- student_id:学生学号
|
||||
- ip:当前IP地址
|
||||
- request_key:生成的请求密钥
|
||||
|
||||
## 验证机制
|
||||
|
||||
### 密钥验证流程
|
||||
|
||||
1. **密钥生成**:
|
||||
- 通过 `/get_signature/` 接口获取密钥
|
||||
- 密钥有效期为5分钟
|
||||
|
||||
2. **密钥验证**:
|
||||
- 在请求 `/query/PaginQuery/` 时需要在URL参数中添加 `key` 参数
|
||||
|
||||
## 异常处理
|
||||
|
||||
- `400`: 缺少密钥参数
|
||||
- `401`: 密钥解析失败或已过期
|
||||
- `403`: IP地址不匹配
|
||||
- `404`: 学生信息不存在或未找到
|
||||
121
Web/ProxyImagesDemo/readme.md
Normal file
121
Web/ProxyImagesDemo/readme.md
Normal file
@@ -0,0 +1,121 @@
|
||||
# 图片代理接口 - README.md
|
||||
## 项目介绍
|
||||
这段代码是基于 **Flask 框架**实现的图片代理接口,核心目标是解决前端页面直接加载第三方图片时出现的**跨域访问限制**问题,同时通过模拟浏览器请求头,提高第三方图片资源的获取成功率,避免被第三方服务器拦截拒绝。
|
||||
|
||||
## 代码核心作用
|
||||
1. **跨域绕过**:作为中间代理服务,接收前端本地请求,再由后端向第三方图片服务器发起请求,规避浏览器的同源策略限制,实现图片正常加载。
|
||||
2. **URL 兼容处理**:自动判断传入的图片地址是否带有 `http://` 或 `https://` 协议头,无协议头时自动补充 `https://`,保证请求 URL 有效性。
|
||||
3. **模拟浏览器请求**:设置符合浏览器规范的 `User-Agent`、`Referer`、`Accept` 请求头,伪装成浏览器正常访问,避免被第三方服务器识别为爬虫而拒绝。
|
||||
4. **图片响应与异常处理**:成功获取图片后,将图片数据以二进制流形式返回给前端,并保留图片原有的 Content-Type;请求失败(状态码非200)或出现异常(超时、网络错误等)时,返回 404 错误。
|
||||
|
||||
## 前置准备
|
||||
1. 已安装 Python 3.6 及以上版本。
|
||||
2. 安装所需的 Python 第三方库:Flask 和 requests。
|
||||
|
||||
## 安装依赖
|
||||
打开终端/命令行,执行以下安装命令:
|
||||
```bash
|
||||
pip install flask requests
|
||||
```
|
||||
|
||||
## 完整可运行代码
|
||||
```python
|
||||
# 导入必要的模块
|
||||
from flask import Flask, send_file, abort
|
||||
import requests
|
||||
from io import BytesIO
|
||||
|
||||
# 初始化 Flask 应用
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/proxy-image/<path:image_url>')
|
||||
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'
|
||||
}
|
||||
|
||||
# 向第三方图片服务器发起请求,设置10秒超时
|
||||
response = requests.get(full_url, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
# 将图片内容存入BytesIO二进制流,用于send_file返回
|
||||
image_data = BytesIO(response.content)
|
||||
# 获取图片原有的Content-Type,无则默认image/jpeg
|
||||
content_type = response.headers.get('Content-Type', 'image/jpeg')
|
||||
# 以二进制流形式返回图片给前端
|
||||
return send_file(image_data, mimetype=content_type)
|
||||
else:
|
||||
# 第三方服务器返回非200状态码,返回404
|
||||
abort(404)
|
||||
except Exception as e:
|
||||
# 捕获超时、网络错误等异常,打印错误信息并返回404
|
||||
print(f"Error fetching image: {e}")
|
||||
abort(404)
|
||||
|
||||
# 启动Flask应用
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
### 步骤1:启动代理服务
|
||||
1. 将完整代码保存为 `image_proxy.py` 文件。
|
||||
2. 终端/命令行进入该文件所在目录,执行以下启动命令:
|
||||
```bash
|
||||
python image_proxy.py
|
||||
```
|
||||
3. 启动成功后,终端会显示类似以下信息,说明代理服务已在本地 5000 端口运行:
|
||||
```
|
||||
* Serving Flask app 'image_proxy'
|
||||
* Debug mode: on
|
||||
* Running on all addresses (0.0.0.0)
|
||||
* Running on http://127.0.0.1:5000
|
||||
* Running on http://192.168.x.xxx:5000
|
||||
```
|
||||
|
||||
### 步骤2:调用代理接口加载图片
|
||||
前端页面或直接在浏览器中,通过以下格式的 URL 调用代理接口,即可加载第三方图片:
|
||||
```
|
||||
http://127.0.0.1:5000/proxy-image/[第三方图片完整URL或无协议头图片URL]
|
||||
```
|
||||
|
||||
#### 调用示例
|
||||
1. 第三方图片完整 URL:`https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg`
|
||||
代理访问地址:
|
||||
```
|
||||
http://127.0.0.1:5000/proxy-image/https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg
|
||||
```
|
||||
2. 无协议头图片 URL:`img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg`
|
||||
代理访问地址(服务自动补充 `https://`):
|
||||
```
|
||||
http://127.0.0.1:5000/proxy-image/img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg
|
||||
```
|
||||
|
||||
#### 前端使用示例(HTML img 标签)
|
||||
```html
|
||||
<!-- 直接加载第三方图片可能出现跨域错误 -->
|
||||
<img src="https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg" alt="电影海报">
|
||||
|
||||
<!-- 通过代理接口加载,规避跨域问题 -->
|
||||
<img src="http://127.0.0.1:5000/proxy-image/https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2807886870.jpg" alt="电影海报">
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
1. **超时设置**:当前请求超时时间为 10 秒,若第三方图片服务器响应较慢,可适当调整 `requests.get` 中的 `timeout` 参数值。
|
||||
2. **请求头调整**:`Referer` 字段当前设置为 `https://movie.douban.com/`,若访问其他域名的图片被拦截,可将 `Referer` 修改为对应第三方图片的域名,或删除该字段。
|
||||
3. **调试模式**:代码中 `app.run` 开启了 `debug=True` 调试模式,生产环境部署时请关闭(改为 `debug=False`),避免泄露敏感信息。
|
||||
4. **跨域配置(可选)**:若前端与代理服务不在同一端口/域名,需在 Flask 应用中添加跨域支持(安装 `flask-cors` 库,配置 `CORS(app)`),否则前端可能无法正常请求代理接口。
|
||||
5. **资源限制**:该代理服务仅用于个人开发/测试场景,大规模商用需考虑图片缓存、并发限制、服务器带宽等问题。
|
||||
Reference in New Issue
Block a user