• C# 下载C站和Libu资源实现逻辑


    下载Libu资源

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Text.RegularExpressions;
    9. using System.Windows.Forms;
    10. using HtmlAgilityPack;
    11. using System.Net;
    12. using System.IO;
    13. using System.Threading;
    14. namespace LoraTool
    15. {
    16. public partial class Form1 : Form
    17. {
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. //读取解析文件1里面的内容
    22. textBox1.Text = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\解析文件1.txt");
    23. }
    24. private void button1_Click(object sender, EventArgs e)
    25. {
    26. //下载对象
    27. WebClient client = new WebClient();
    28. client.Encoding = Encoding.UTF8;
    29. this.button1.Text = "处理中...";
    30. this.button1.Enabled = false;
    31. string input = textBox1.Text;
    32. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    33. doc.LoadHtml(input); //加载html
    34. HtmlNode node = doc.DocumentNode;
    35. // 代码中的.//img[@src] 是 XPath 路径代码,意思就是返回所有超链接
    36. HtmlNodeCollection aTags = node.SelectNodes(".//a[@href]"); //获取所有超链接
    37. int que = 0;
    38. foreach (var item in aTags)
    39. {
    40. try
    41. {
    42. que++;
    43. string name = item.SelectSingleNode(".//span[@class='modelList_subtitle__kCBID']").InnerText;//获取名字
    44. string imgUrl = item.SelectSingleNode(".//img[@src]").Attributes["src"].Value.Split('?')[0];//图片地址
    45. string infoUrl = "https://www.liblib.ai" + item.Attributes["href"].Value; //获得地址,用来获取下载地址
    46. //获取下载地址
    47. WebClient webClient = new WebClient();
    48. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    49. Stream myStream = webClient.OpenRead(infoUrl);
    50. StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
    51. string strHTML = sr.ReadToEnd();
    52. myStream.Close();
    53. Match match = Regex.Match(strHTML, "https://liblibai-online.vibrou.com/[0-9|a-z|A-Z|/]*.safetensors");
    54. string downUrl = match.Value;
    55. //去掉文件名中的特殊字符
    56. foreach (var c in Path.GetInvalidFileNameChars())
    57. {
    58. name = name.Replace(c.ToString(), string.Empty);
    59. }
    60. //命名
    61. name = que + "_" + name;
    62. //新建保存地址
    63. string folder = Environment.CurrentDirectory + "\\Libu";
    64. if (!Directory.Exists(folder))
    65. {
    66. Directory.CreateDirectory(folder);
    67. }
    68. //输出
    69. textBox2.AppendText("序号:" + que + "/" + aTags.Count);
    70. textBox2.AppendText("\r\n名称:" + name);
    71. textBox2.AppendText("\r\n图片地址:" + imgUrl);
    72. textBox2.AppendText("\r\n详情地址:" + infoUrl);
    73. textBox2.AppendText("\r\n下载地址:" + downUrl);
    74. if (string.IsNullOrEmpty(imgUrl) || string.IsNullOrEmpty(infoUrl) || string.IsNullOrEmpty(downUrl))
    75. {
    76. que--;
    77. textBox2.AppendText("\r\n执行异常:" + "ImgUrl或InfoUrl或DownUrl解析为空");
    78. continue;
    79. }
    80. //下载图片
    81. client.DownloadFile(imgUrl, folder + "\\" + name + Path.GetExtension(imgUrl));
    82. //下载Lora模型
    83. client.DownloadFile(downUrl, folder + "\\" + name + Path.GetExtension(downUrl));
    84. textBox2.AppendText("\r\n执行成功");
    85. }
    86. catch (Exception ex)
    87. {
    88. que--;
    89. textBox2.AppendText("\r\n执行异常:" + ex.Message);
    90. }
    91. textBox2.AppendText("\r\n------------------------\r\n");
    92. }
    93. System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\Libu执行结果.txt", textBox2.Text);
    94. this.button1.Text = "解析内容";
    95. this.button1.Enabled = true;
    96. }
    97. }
    98. }

    下载C站实现逻辑

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Text.RegularExpressions;
    9. using System.Windows.Forms;
    10. using System.Net;
    11. using System.IO;
    12. using System.Threading;
    13. using Newtonsoft.Json;
    14. using HtmlAgilityPack;
    15. namespace LoraTool
    16. {
    17. public class CSite
    18. {
    19. public int id { get; set; }
    20. public string name { get; set; }
    21. }
    22. public partial class Form2 : Form
    23. {
    24. public Form2()
    25. {
    26. InitializeComponent();
    27. //读取解析文件1里面的内容
    28. string json = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\解析文件2.txt");
    29. // 将JSON字符串转换为C#对象
    30. List<CSite> person = JsonConvert.DeserializeObject<List<CSite>>(json);
    31. textBox1.Text = JsonConvert.SerializeObject(person);
    32. }
    33. private void button1_Click(object sender, EventArgs e)
    34. {
    35. //下载对象
    36. WebClient client = new WebClient();
    37. client.Encoding = Encoding.UTF8;
    38. this.button1.Text = "处理中...";
    39. this.button1.Enabled = false;
    40. string input = textBox1.Text;
    41. // 将JSON字符串转换为C#对象
    42. List<CSite> items = JsonConvert.DeserializeObject<List<CSite>>(input);
    43. int que = 0;
    44. foreach (var item in items)
    45. {
    46. try
    47. {
    48. que++;
    49. //if (item.id != 25537) continue;
    50. string name = item.name;//获取名字
    51. string infoUrl = "https://civitai.com/models/" + item.id; //获得地址,用来获取下载地址
    52. //获取下载地址
    53. WebClient webClient = new WebClient();
    54. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    55. Stream myStream = webClient.OpenRead(infoUrl);
    56. StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
    57. string strHTML = sr.ReadToEnd();
    58. myStream.Close();
    59. Match match = Regex.Match(strHTML, "/api/download/models/[0-9]*");
    60. //下载地址
    61. string downUrl = "https://civitai.com" + match.Value;
    62. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    63. doc.LoadHtml(strHTML); //加载html
    64. HtmlNode node = doc.DocumentNode;
    65. string imgUrl = node.SelectSingleNode(".//img[@class='mantine-7aj0so']").Attributes["src"].Value;//图片地址
    66. //去掉文件名中的特殊字符
    67. foreach (var c in Path.GetInvalidFileNameChars())
    68. {
    69. name = name.Replace(c.ToString(), string.Empty);
    70. }
    71. //命名
    72. name = que + "_" + name;
    73. //新建保存地址
    74. string folder = Environment.CurrentDirectory + "\\CSite";
    75. if (!Directory.Exists(folder))
    76. {
    77. Directory.CreateDirectory(folder);
    78. }
    79. //输出
    80. textBox2.AppendText("序号:" + que + "/" + items.Count);
    81. textBox2.AppendText("\r\n名称:" + name);
    82. textBox2.AppendText("\r\n图片地址:" + imgUrl);
    83. textBox2.AppendText("\r\n详情地址:" + infoUrl);
    84. textBox2.AppendText("\r\n下载地址:" + downUrl);
    85. if (string.IsNullOrEmpty(imgUrl) || string.IsNullOrEmpty(infoUrl) || string.IsNullOrEmpty(downUrl))
    86. {
    87. que--;
    88. textBox2.AppendText("\r\n执行异常:" + "ImgUrl或InfoUrl或DownUrl解析为空");
    89. continue;
    90. }
    91. //下载图片
    92. client.DownloadFile(imgUrl, folder + "\\" + name + Path.GetExtension(imgUrl));
    93. //下载Lora模型
    94. client.DownloadFile(downUrl, folder + "\\" + name + ".safetensors");
    95. textBox2.AppendText("\r\n执行成功");
    96. }
    97. catch (Exception ex)
    98. {
    99. que--;
    100. textBox2.AppendText("\r\n执行异常:" + ex.Message);
    101. }
    102. textBox2.AppendText("\r\n------------------------\r\n");
    103. }
    104. System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\C站执行结果.txt", textBox2.Text);
    105. this.button1.Text = "解析内容";
    106. this.button1.Enabled = true;
    107. }
    108. }
    109. }

  • 相关阅读:
    LeetCode算法题解(回溯、难点)|LeetCode332. 重新安排行程
    全套完整版实战型Java云HIS系统源码
    git commit 提交校验详解
    PDF格式分析(六十八)——注释(批注)概要
    为什么非const静态成员变量一定要在类外定义
    视频播放压缩的相关知识点:I帧、P帧、B帧、RTMP协议、RTSP协议、GB28181协议等学习记录
    【ocean】报错*Error* quotient: can‘t handle (nil / nil)
    MATLAB算法实战应用案例精讲-【大模型】LLM算法(补充篇)
    Neo4j在linux上的安装与Springboot的集成
    我是怎么从软件测试转到自动化测试岗的?亲身经历分享
  • 原文地址:https://blog.csdn.net/qq873113580/article/details/133757671