Browse Source

网页解析测试

Xiao_123 2 tuần trước cách đây
mục cha
commit
a4a9186fb5

+ 13 - 0
ecosystem.config.js

@@ -0,0 +1,13 @@
+module.exports = {
+  apps: [{
+    name: 'my-node-server',      // 服务名称
+    script: 'server/server.js',  // 入口文件
+    instances: 'max',            // 使用所有CPU核心
+    autorestart: true,           // 崩溃后自动重启
+    watch: false,                // 不监听文件变化(生产环境禁用)
+    env: {
+      NODE_ENV: 'demo',    			// 环境变量
+      PORT: 9998                // 端口
+    }
+  }]
+};

+ 4 - 1
package.json

@@ -23,7 +23,8 @@
     "lint:eslint": "eslint --fix --ext .js,.ts,.vue ./src",
     "lint:format": "prettier --write --loglevel warn \"src/**/*.{js,ts,json,tsx,css,less,scss,vue,html,md}\"",
     "lint:style": "stylelint --fix \"./src/**/*.{vue,less,postcss,css,scss}\" --cache --cache-location node_modules/.cache/stylelint/",
-    "lint:lint-staged": "lint-staged -c "
+    "lint:lint-staged": "lint-staged -c ",
+    "start": "pm2 start ecosystem.config.js"
   },
   "dependencies": {
     "@element-plus/icons-vue": "^2.1.0",
@@ -42,6 +43,7 @@
     "benz-amr-recorder": "^1.1.5",
     "bpmn-js-token-simulation": "^0.10.0",
     "camunda-bpmn-moddle": "^7.0.1",
+    "cors": "^2.8.5",
     "cropperjs": "^1.6.1",
     "crypto-js": "^4.2.0",
     "dayjs": "^1.11.10",
@@ -50,6 +52,7 @@
     "echarts": "^5.5.0",
     "echarts-wordcloud": "^2.1.0",
     "element-plus": "2.6.1",
+    "express": "^5.1.0",
     "fast-xml-parser": "^4.3.2",
     "highlight.js": "^11.9.0",
     "js-base64": "^3.7.7",

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 408 - 0
pnpm-lock.yaml


+ 54 - 0
server/server.js

@@ -0,0 +1,54 @@
+const express = require('express');
+const axios = require('axios');
+const app = express();
+
+const cors = require('cors');
+app.use(cors());
+
+// 中间件,用于解析 JSON 请求体
+app.use(express.json());
+
+// 接口路由
+app.post('/process-urls', async (req, res) => {
+  try {
+    const { urlArr } = req.body;
+        
+    if (!urlArr || !Array.isArray(urlArr)) {
+      return res.status(400).json({ error: 'urlArr 必须是一个数组' });
+    }
+
+    const requests = urlArr.map(url => {
+      return axios.get(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://mp.weixin.qq.com/',
+          'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
+        }
+      })
+    	.then(res => ({
+        url: url,
+        data: res.data
+      }))
+      .catch(error => ({
+        url: url,
+        error: error.message
+      }));
+    });
+
+    const results = await Promise.all(requests);
+        
+    // 过滤掉没有数据的项
+    const contents = results.filter(item => item.data);
+        
+    res.json({ contents });
+  } catch (error) {
+    console.error('处理请求时出错:', error);
+    res.status(500).json({ error: '服务器内部错误' });
+  }
+});
+
+// 启动服务器
+const PORT = 9998;
+app.listen(PORT, () => {
+  console.log(`服务器运行在 http://localhost:${PORT}`);
+});

+ 3 - 11
src/views/menduner/system/talentMap/maintenance/gather/webPageParsing/index.vue

@@ -183,20 +183,12 @@ const handleExecute = async () => {
 			return
 		}
 
-		const requests = urlArr.map(url => {
-			const path = url.split('mp.weixin.qq.com')[1]
-			const realurl = `/weixin-article${path}`
-			return axios.get(realurl).then(res => ({
-				url: url,
-				data: res.data
-			}))
+		axios.post('/process-urls', { urlArr }).then(res => {
+			contents.value = res?.data?.contents ?? []
 		})
 
-		const results = await Promise.all(requests)
-		contents.value = results.filter(item => item.data)
 	} catch (e) {
-		console.error(e)
-		message.error('获取失败')
+		message.error('获取失败', e)
 	} finally {
 		loading.value = false
 	}

+ 9 - 4
vite.config.ts

@@ -46,11 +46,16 @@ export default ({ command, mode }: ConfigEnv): UserConfig => {
           changeOrigin: true, // 是否支持跨域
           rewrite: (path) => path.replace(new RegExp(`^/api`), '/api')
         },
-        '/weixin-article': {
-          target: 'https://mp.weixin.qq.com/',
+        // '/weixin-article': {
+        //   target: 'https://mp.weixin.qq.com/',
+        //   secure: true,
+        //   changeOrigin: true,
+        //   rewrite: (path) => path.replace(new RegExp(`^/weixin-article`), '')
+        // },
+        '/process-urls': {
+          target: 'http://localhost:9998',
           secure: true,
-          changeOrigin: true,
-          rewrite: (path) => path.replace(new RegExp(`^/weixin-article`), '')
+          changeOrigin: true
         }
       }
     },

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác