C# 批量替换html里面的http链接

C# 批量替换html里面的http链接

python 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;

namespace 批量替换文件html
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_chose_Click(object sender, EventArgs e)
        {
            string selectedFolderPath = SelectFolder();
            if (!string.IsNullOrEmpty(selectedFolderPath))
            {
                // 用户选择了文件夹,并且可以使用selectedFolderPath
                //MessageBox.Show("选择的文件夹路径: " + selectedFolderPath);
                tbx_LJ.Text = selectedFolderPath;
            }
            else
            {
                MessageBox.Show("没有选择文件夹");
            }
        }

        public static string SelectFolder()
        {
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "请选择一个文件夹";
                DialogResult dialogResult = folderBrowserDialog.ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    return folderBrowserDialog.SelectedPath;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        public int count = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = @tbx_LJ.Text;
            string[] htmlFiles = Directory.GetFiles(folderPath, "*.html", SearchOption.AllDirectories);

            foreach (string file in htmlFiles)
            {

                textBox1.AppendText(file+"\r\n");

                count++;
                lbl_count.Text = count.ToString() + "个";
                Application.DoEvents();
                //timer1.Enabled = true;
                string content = File.ReadAllText(file);
                content = Regex.Replace(content, @"href='https:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);
                // // 删除href属性中的http链接
                content = Regex.Replace(content, @"href=""http:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"url\(http:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""http:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                // // 删除href属性中的http链接
                content = Regex.Replace(content, @"href=""https:\/\/[^""]+", "href=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"url\(https:\/\/[^)]+\)", "url(#)", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                // //删除style属性中的url里的http链接
                content = Regex.Replace(content, @"src=""https:\/\/[^""]+", "src=\"", RegexOptions.IgnoreCase);

                content = Regex.Replace(content, @"action=""https:\/\/[^""]+", "action=\"", RegexOptions.IgnoreCase);

                content = Regex.Replace(content, @"hm.src=""https:\/\/[^""]+", "hm.src=\"", RegexOptions.IgnoreCase);

                File.WriteAllText(file, content);
                //timer1.Enabled = true;
            }

        }
        public string filename;
        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

源码下载:

https://download.csdn.net/download/weixin_43050480/90140030

相关推荐
土了个豆子的3 小时前
04.事件中心模块
开发语言·前端·visualstudio·单例模式·c#
@areok@4 小时前
C++mat传入C#OpencvCSharp的mat
开发语言·c++·opencv·c#
Yvonne爱编码4 小时前
AJAX入门-AJAX 概念和 axios 使用
前端·javascript·ajax·html·js
果壳~4 小时前
【Python】爬虫html提取内容基础,bs4
爬虫·python·html
high20115 小时前
【 运维相关】-- HTTP 压测/负载发生器之新秀 oha
运维·网络协议·http
时光追逐者6 小时前
C# 哈希查找算法实操
算法·c#·哈希算法
三千道应用题7 小时前
C#语言入门详解(18)传值、输出、引用、数组、具名、可选参数、扩展方法
开发语言·c#
micoos7 小时前
C#-LinqToObject-Element
c#
忧郁的蛋~7 小时前
使用.NET标准库实现多任务并行处理的详细过程
开发语言·c#·.net
AD钙奶-lalala7 小时前
HTTP response code 200 206 416详解
网络·网络协议·http