标签不加C#代码采集到的是”http://img.111.com/IMG/odi1b23569/odi1b4cd5c/w1hjqfc6f6/0080_mda4ma.jpg”, “http://img.11.com/IMG/odi1b23569/odi1b4cd5c/w1hjqfc6f6/0083_mda4mw.jpg”, “http://img.11.com/IMG/di1b23569/odi1b4cd5c/w1hjqfc6f6/0084_mda4na.jpg”, “http://img.11.com/IMG/odi1b23569/odi1b4cd5c/w1hjqfc6f6/0085_mda4nq.jpg”,复制代码 但是加C#代码返回的值是空的 using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using SpiderInterface; class LocoyCode { ///
/// 执行方法,不能修改类和方法名称。 ///
/// 标签内容 /// 页面响应,包含了Url、原始Html等属性 /// 返回处理后的标签内容 public string Run(string content, ResponseEntry response) { // 初始化urls列表 List urls = new List(); // 分割content以获取每个URL(假设每行一个URL) string[] contentLines = content.Split(new[] { ‘\r’, ‘\n’ }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in contentLines) { // 添加每行的URL到urls列表 urls.Add(line); } // 生成固定的MD5值 string fixedMd5Value = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(“fixed_md5_value”))).Replace(“-“, “”).ToLower().Substring(0, 6); foreach (string url in urls) { Match domainMatch = Regex.Match(url, @”^https?://(.*?)/”); if (domainMatch.Success) { string domain = domainMatch.Groups[1].Value; // 替换域名并去掉http://或https:// string replacedUrl = url.Replace(domain, “rsync -avzut –progress /cygdrive/e/wwwroot/img.1.com/”).Replace(“http://”, “”).Replace(“https://”, “”); // 提取文件扩展名 Match fileExtensionMatch = Regex.Match(url, @”\.(\w+)$”); if (fileExtensionMatch.Success) { string fileExtension = fileExtensionMatch.Groups[1].Value; // 生成随机的20字符的字母数字字符串 string randomStr = GenerateRandomString(20); // 创建新的URL格式,加上空格 string newUrl = $”{replacedUrl} /cygdrive/e/wwwroot/pic/wp-content/uploads/2024/01/19/{fixedMd5Value}/{randomStr}.{fileExtension}”; // 在这里可以将新的URL或其他处理后的数据用于content的进一步处理 content = newUrl; // 举例,可以根据需要更改 } } } // 返回处理后的content return content; } static string GenerateRandomString(int length) { const string chars = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”; var random = new Random(); var result = new StringBuilder(length); for (int i = 0; i < length; i++) { result.Append(chars[random.Next(chars.Length)]); } return result.ToString(); } } 复制代码
没有回复内容