{"id":125,"date":"2024-12-03T16:57:47","date_gmt":"2024-12-03T08:57:47","guid":{"rendered":"https:\/\/blog.rensr.site\/?p=125"},"modified":"2024-12-03T16:57:50","modified_gmt":"2024-12-03T08:57:50","slug":"%e5%b0%8f%e7%88%b1%e8%af%be%e7%a8%8b%e8%a1%a8%e5%af%bc%e5%85%a5%e5%8a%9f%e8%83%bd%e4%bc%98%e5%8c%96%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/blog.rensr.site\/index.php\/2024\/12\/03\/%e5%b0%8f%e7%88%b1%e8%af%be%e7%a8%8b%e8%a1%a8%e5%af%bc%e5%85%a5%e5%8a%9f%e8%83%bd%e4%bc%98%e5%8c%96%e5%88%86%e6%9e%90\/","title":{"rendered":"\u5c0f\u7231\u8bfe\u7a0b\u8868\u5bfc\u5165\u529f\u80fd\u4f18\u5316\u5206\u6790"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u63cf\u8ff0<\/h2>\n\n\n\n<p>\u5728\u4e0a\u4e00\u7248\u672c\u7684\u8bfe\u8868\u5bfc\u5165\u529f\u80fd\u4e2d\uff0c\u51fa\u73b0\u4e86 &#8220;day \u4e0d\u5b58\u5728\u6216\u503c\u4e3a\u7a7a&#8221; \u7684\u9519\u8bef\u3002\u8fd9\u4e2a\u95ee\u9898\u4e3b\u8981\u51fa\u73b0\u5728 Parser.js \u4e2d\u89e3\u6790\u8bfe\u7a0b\u8868\u6570\u636e\u7684\u8fc7\u7a0b\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u539f\u56e0\u5206\u6790<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5b9a\u4f4d\u95ee\u9898<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e7\u7248\u672c\u4f7f\u7528 <code>td:eq(18)<\/code> \u6765\u83b7\u53d6\u661f\u671f\u51e0\u4fe1\u606f<\/li>\n\n\n\n<li>\u8fd9\u4e2a\u7d22\u5f15\u4f4d\u7f6e\u53ef\u80fd\u56e0\u4e3a\u7cfb\u7edf\u66f4\u65b0\u5bfc\u81f4\u4e0d\u51c6\u786e\uff0c\u5bfc\u81f4\u83b7\u53d6\u5230\u7a7a\u503c\u6216\u9519\u8bef\u7684\u6587\u672c\u5185\u5bb9<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u683c\u5f0f\u95ee\u9898<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65e7\u7248\u672c\u4f7f\u7528 <code>charAt(0)<\/code> \u76f4\u63a5\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26<\/li>\n\n\n\n<li>\u6ca1\u6709\u5bf9\u6587\u672c\u683c\u5f0f\u8fdb\u884c\u5145\u5206\u7684\u9a8c\u8bc1\u548c\u5904\u7406<\/li>\n\n\n\n<li>\u53ef\u80fd\u9047\u5230\u7a7a\u5b57\u7b26\u4e32\u6216\u683c\u5f0f\u4e0d\u7b26\u5408\u9884\u671f\u7684\u60c5\u51b5<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9519\u8bef\u5904\u7406\u4e0d\u8db3<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53 <code>parseDay<\/code> \u8fd4\u56de undefined \u65f6\uff0c\u6ca1\u6709\u5408\u9002\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/li>\n\n\n\n<li>\u5bfc\u81f4\u6574\u4e2a\u8bfe\u7a0b\u5bf9\u8c61\u7684 day \u5c5e\u6027\u4e3a\u7a7a<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<p>\u65b0\u7248\u672c\u7684\u6539\u8fdb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function parseDay(dayText) {\n    const dayMap = {\n        '\u4e00': 1,\n        '\u4e8c': 2,\n        '\u4e09': 3,\n        '\u56db': 4,\n        '\u4e94': 5,\n        '\u516d': 6,\n        '\u65e5': 7\n    };\n\n    \/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d \u5468\u51e0\n    const match = dayText.match(\/^(.)(?=\\&#91;\\d)\/);\n    if(!match) return null;\n\n    return dayMap&#91;match&#91;1]] || null;\n}<\/code><\/pre>\n\n\n\n<p>\u4e3b\u8981\u6539\u8fdb\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u66f4\u7cbe\u786e\u7684\u6570\u636e\u63d0\u53d6<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>\/^(.)(?=\\[\\d)\/<\/code> \u6765\u5339\u914d\u683c\u5f0f<\/li>\n\n\n\n<li>\u786e\u4fdd\u63d0\u53d6\u7684\u662f\u8282\u6b21\u4fe1\u606f\u524d\u7684\u661f\u671f\u51e0\u5b57\u7b26<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u66f4\u4e25\u683c\u7684\u6570\u636e\u9a8c\u8bc1<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u6709\u5b8c\u5168\u5339\u914d\u9884\u671f\u683c\u5f0f\u624d\u4f1a\u8fd4\u56de\u7ed3\u679c<\/li>\n\n\n\n<li>\u5426\u5219\u8fd4\u56de null\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b8c\u5584\u7684\u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   if (course.name &amp;&amp; course.teacher &amp;&amp; course.position &amp;&amp; \n       course.weeks.length &amp;&amp; course.sections.length &amp;&amp; course.day) {\n       result.push(course);\n   }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u6709\u6240\u6709\u5fc5\u8981\u4fe1\u606f\u90fd\u5b58\u5728\u65f6\u624d\u6dfb\u52a0\u8bfe\u7a0b<\/li>\n\n\n\n<li>\u907f\u514d\u4e0d\u5b8c\u6574\u6570\u636e\u8fdb\u5165\u7ed3\u679c\u96c6<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6548\u679c\u5bf9\u6bd4<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65e7\u7248\u672c\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u80fd\u4ea7\u751f\u4e0d\u5b8c\u6574\u7684\u8bfe\u7a0b\u6570\u636e<\/li>\n\n\n\n<li>\u5bb9\u6613\u51fa\u73b0 day \u4e3a\u7a7a\u7684\u60c5\u51b5<\/li>\n\n\n\n<li>\u9519\u8bef\u5904\u7406\u673a\u5236\u4e0d\u5b8c\u5584<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b0\u7248\u672c\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u63d0\u53d6\u66f4\u51c6\u786e<\/li>\n\n\n\n<li>\u4e25\u683c\u7684\u6570\u636e\u9a8c\u8bc1<\/li>\n\n\n\n<li>\u5b8c\u5584\u7684\u9519\u8bef\u5904\u7406<\/li>\n\n\n\n<li>\u4e0d\u4f1a\u51fa\u73b0 day \u4e3a\u7a7a\u7684\u60c5\u51b5<\/li>\n<\/ul>\n\n\n\n<p>\u3010scheduleHtmlParser.js\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ScheduleParser {\n  \/\/ \u89e3\u6790cron\u8868\u8fbe\u5f0f\n  static parseCron(expression) {\n    \/\/ \u89e3\u6790\u903b\u8f91\n  }\n\n  \/\/ \u89e3\u6790\u8c03\u5ea6\u914d\u7f6e\n  static parseConfig(config) {\n    return {\n      taskId: config.taskId,\n      cron: this.parseCron(config.cron),\n      timeout: config.timeout || 3000\n    }\n  }\n}\n\nfunction scheduleHtmlParser(html) {\n    if (!html) return &#91;];\n    \n    const $ = cheerio.load(html, { decodeEntities: false });\n    const result = &#91;];\n    \n    \/\/ \u904d\u5386\u6bcf\u4e00\u884c\u8bfe\u7a0b\u4fe1\u606f\n    $('tbody tr').each(function() {\n        const course = {\n            name: $(this).find('td:nth-child(5)').text().trim().replace(\/^\\&#91;\\d+\\]\/, ''),\n            teacher: $(this).find('td:nth-child(15)').text().trim().replace(\/^\\&#91;\\d+\\]\/, ''),\n            position: $(this).find('td:nth-child(21)').text().trim(),\n            weeks: parseWeeks($(this).find('td:nth-child(18)').text().trim()),\n            sections: parseSections($(this).find('td:nth-child(20)').text().trim()),\n            day: parseDay($(this).find('td:nth-child(20)').text().trim())\n        };\n\n        \/\/ \u53ea\u6dfb\u52a0\u6709\u6548\u7684\u8bfe\u7a0b\u4fe1\u606f\n        if (course.name &amp;&amp; course.teacher &amp;&amp; course.position &amp;&amp; \n            course.weeks.length &amp;&amp; course.sections.length &amp;&amp; course.day) {\n            result.push(course);\n        }\n    });\n\n    return result;\n}\n\n\/\/ \u89e3\u6790\u5468\u6570\uff0c\u652f\u6301\u5355\u4e2a\u5468\u548c\u8303\u56f4\uff0c\u5982\"1\"\u6216\"1-12\"\nfunction parseWeeks(weeksText) {\n    const weeks = &#91;];\n    const parts = weeksText.split(',');\n    \n    for(let part of parts) {\n        if(part.includes('-')) {\n            const &#91;start, end] = part.split('-').map(Number);\n            for(let i = start; i &lt;= end; i++) {\n                weeks.push(i);\n            }\n        } else {\n            const week = parseInt(part);\n            if(!isNaN(week)) {\n                weeks.push(week);\n            }\n        }\n    }\n    \n    return weeks;\n}\n\n\/\/ \u89e3\u6790\u8282\u6b21\uff0c\u5982\"\u4e00&#91;8-9\u8282]\"\u4e2d\u76848-9\nfunction parseSections(sectionText) {\n    const match = sectionText.match(\/\\&#91;(\\d+)-(\\d+)\u8282\\]\/);\n    if(!match) return &#91;];\n    \n    const start = parseInt(match&#91;1]);\n    const end = parseInt(match&#91;2]);\n    return Array.from({length: end - start + 1}, (_, i) => start + i);\n}\n\n\/\/ \u89e3\u6790\u661f\u671f\u51e0\uff0c\u5982\"\u4e00&#91;8-9\u8282]\"\u4e2d\u7684\"\u4e00\"\nfunction parseDay(dayText) {\n    const dayMap = {\n        '\u4e00': 1,\n        '\u4e8c': 2,\n        '\u4e09': 3,\n        '\u56db': 4,\n        '\u4e94': 5,\n        '\u516d': 6,\n        '\u65e5': 7\n    };\n    \n    const match = dayText.match(\/^(.)(?=\\&#91;\\d)\/);\n    if(!match) return null;\n    \n    return dayMap&#91;match&#91;1]] || null;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u8fd9\u6b21\u4f18\u5316\u4e3b\u8981\u901a\u8fc7\u6539\u8fdb\u6570\u636e\u89e3\u6790\u903b\u8f91\u548c\u589e\u5f3a\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u89e3\u51b3\u4e86 day \u4e3a\u7a7a\u7684\u95ee\u9898\u3002\u65b0\u7248\u672c\u7684\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u4f9b\u66f4\u53ef\u9760\u7684\u8bfe\u8868\u5bfc\u5165\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0 \u5728\u4e0a\u4e00\u7248\u672c\u7684\u8bfe\u8868\u5bfc\u5165\u529f\u80fd\u4e2d\uff0c\u51fa\u73b0\u4e86 &#8220;day \u4e0d\u5b58\u5728\u6216\u503c\u4e3a\u7a7a&#8221; \u7684\u9519\u8bef\u3002\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-my-project"],"_links":{"self":[{"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":1,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rensr.site\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}