当前位置: 首页 > wzjs >正文

江苏省建设厅网站查询简易网站制作

江苏省建设厅网站查询,简易网站制作,局域网站建设教程,一站式网络营销说明:我希望用nodejs 写一个小工具,去除本地txt文件中的html字符,去除字符后生成新的文件,同样保存在桌面 文件的具体位置是C:\Users\Administrator\Desktop\file.txt step1:C:\Users\Administrator\WebstormProjects\untitled4\f…

说明:我希望用nodejs 写一个小工具,去除本地txt文件中的html字符,去除字符后生成新的文件,同样保存在桌面 文件的具体位置是C:\Users\Administrator\Desktop\file.txt
step1:C:\Users\Administrator\WebstormProjects\untitled4\file.js

const fs = require('fs');
const path = require('path');
// 定义文件路径
const desktopPath = 'C:\\Users\\Administrator\\Desktop';
const inputFile = path.join(desktopPath, 'file.txt');
const outputFile = path.join(desktopPath, 'clean_file.txt');
// 自定义HTML实体替换规则
const htmlEntities = {'&lt;': '<','&gt;': '>','&amp;': '&','&quot;': '"','&apos;': "'",'&#39;': "'",'&nbsp;': ' ',
};
// 主处理函数
function cleanHtml(filePath) {try {// 读取文件内容const content = fs.readFileSync(filePath, 'utf8');// 分步骤清理内容let cleaned = content// 移除HTML标签.replace(/<[^>]+>/g, '')// 替换HTML实体.replace(/&(?:[a-z]+|#\d+);/gi, (match) =>htmlEntities[match.toLowerCase()] || match)// 清理多余换行和空格.replace(/\n{3,}/g, '\n\n').replace(/ {2,}/g, ' ');// 写入新文件fs.writeFileSync(outputFile, cleaned, 'utf8');console.log(`文件清理完成,已保存至:${outputFile}`);console.log(`清理前字符数:${content.length}`);console.log(`清理后字符数:${cleaned.length}`);console.log(`移除字符数:${content.length - cleaned.length}`);} catch (error) {console.error('处理文件时发生错误:');console.error(error.message);process.exit(1);}
}
// 执行清理
if (require.main === module) {// 检查源文件是否存在if (!fs.existsSync(inputFile)) {console.error(`错误:源文件 ${inputFile} 不存在`);process.exit(1);}console.log('正在清理HTML字符...');cleanHtml(inputFile);
}

step2: 运行

PS C:\Users\Administrator\WebstormProjects\untitled4> node file.js
正在清理HTML字符...
文件清理完成,已保存至:C:\Users\Administrator\Desktop\clean_file.txt
清理前字符数:2235
清理后字符数:1697
移除字符数:538

下面是用python实现同样的功能 C:\Users\Administrator\PycharmProjects\PythonProject2.venv\Scripts\activate_this.py

from pathlib import Path
import re
import sys# 定义文件路径
desktop_path = Path(r'C:\Users\Administrator\Desktop')
input_file = desktop_path / 'file.txt'
output_file = desktop_path / 'cleans_file.txt'# 自定义HTML实体替换规则
html_entities = {'&lt;': '<','&gt;': '>','&amp;': '&','&quot;': '"','&apos;': "'",'&#39;': "'",'&nbsp;': ' ',
}def clean_html(file_path):try:# 读取文件内容with open(file_path, 'r', encoding='utf-8') as f:content = f.read()# 分步骤清理内容cleaned = content# 移除HTML标签cleaned = re.sub(r'<[^>]+>', '', cleaned)# 替换HTML实体def replace_entity(match):entity = match.text.lower()return html_entities.get(entity, match.text)cleaned = re.sub(r'&(?:[a-z]+|#\d+);',replace_entity,cleaned,flags=re.IGNORECASE)# 清理多余换行和空格cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)cleaned = re.sub(r' {2,}', ' ', cleaned)# 写入新文件with open(output_file, 'w', encoding='utf-8') as f:f.write(cleaned)print(f'文件清理完成,已保存至:{output_file}')print(f'清理前字符数:{len(content)}')print(f'清理后字符数:{len(cleaned)}')print(f'移除字符数:{len(content) - len(cleaned)}')except Exception as e:print('处理文件时发生错误:')print(f'{e}')sys.exit(1)if __name__ == "__main__":# 检查源文件是否存在if not input_file.exists():print(f'错误:源文件 {input_file} 不存在')sys.exit(1)print('正在清理HTML字符...')clean_html(input_file)

end

http://www.dtcms.com/wzjs/329399.html

相关文章:

  • asp.net做电商网站页面网络营销课程
  • 潍坊商城网站建设周口搜索引擎优化
  • http做轮播图网站推广软文范例100字
  • 有风险的网站搜索量排行
  • 投资网站建设合肥网络推广软件
  • 洋气的设计公司名字企业网站如何优化
  • 做招标网站 如何营销网站都有哪些
  • 室内设计做效果图可以接单的网站沈阳seo
  • 海珠区网站建设seo的优化步骤
  • 虹口做网站公司百度金融
  • 网站制作布局营销型网站建设的重要原则
  • 滨海做网站价格sem是什么意思?
  • 佛山微信网站建设建站公司
  • 怎么做最火的视频网站武汉关键词排名推广
  • 平原做网站宁波seo外包服务商
  • 科技网站颜色百度推广基木鱼
  • 东莞厚街创新科技职业学院安卓优化大师官方版本下载
  • 网站后台操作手册舟山百度seo
  • 在百度上如何上传自己的网站网站搜索引擎优化报告
  • 做钓鱼网站用哪种编程语言最吸引人的营销广告文案
  • 爱站长尾关键词挖掘工具全网推广系统
  • 新开传奇网站合击seo全称是什么
  • 怎么黑网站的步骤seo资料站
  • 个人网站的留言板数据库怎么做百度163黄页关键词挖掘
  • 番禺做网站的百度关键词推广工具
  • 现在哪个网站做电商好怎么样才可以在百度上打广告
  • 网站作风建设年专栏百度关键词推广可以自己做吗
  • python做网站教程河南专业网络推广公司
  • 网站APP注册做任务怎么自己制作网页
  • 国外做美食视频网站网站内部seo优化包括