c# - 将链接存储到变量而不是文本文件中
问题描述
我处于 C# 的早期学习曲线上。我有一个将网络链接存储到文本文件中的代码。如何将它们存储到变量中,以便稍后在代码中循环它们并分别访问每一个?
string pdfLinksUrl = "https://www.nordicwater.com/products/waste-water/";
// Load HTML content
var webGet = new HtmlAgilityPack.HtmlWeb();
var doc = webGet.Load(pdfLinksUrl);
// select all <A> nodes from the document using XPath
// (unfortunately we can't select attribute nodes directly as
// it is not yet supported by HAP)
var linkNodes = doc.DocumentNode.SelectNodes("//a[@href]");
// select all href attribute values ending with '.pdf' (case-insensitive)
var pdfUrls = from linkNode in linkNodes
let href = linkNode.Attributes["href"].Value
where href.ToLower().StartsWith("https://www.nordicwater.com/product/")
select href;
// write all PDF links to file
System.IO.File.WriteAllLines(@"c:\temp\pdflinks.txt", pdfUrls.ToArray());
解决方案
pdfUrls
保存您的所有 URL,当您将所有 URL 写入文件时,您正在使用它
您可以使用 foreach 循环来轻松地遍历 URL:
foreach (string url in odfUrls.ToArray()) {
Console.WriteLine($"PDF URL: {url}");
}
推荐阅读
- reactjs - 无法读取未定义的属性“handleChange”
- mongodb - Mongo 聚合,对数组元素调用 $match 不起作用。(包括复制)
- excel - 如何仅计算给定日期和时间之间的小时和分钟?
- java - Digester Parsing 未将 bean 添加到主 Bean
- c# - 如何修复“LINQ to Entities 无法识别方法‘System.DateTime GetValueOrDefault()’”?
- firebase - 如何防止有人在我的数据库中过度执行查询?
- python-3.x - Python:Writelines - 一直到边距
- javascript - 在 R Sortable 中为可排序对象文本添加索引
- go - 为什么这个函数在声明时有三个部分?
- python - 在数据框中的 Pandas 系列上的空格前获取字符串