【发布时间】:2020-06-21 22:25:35
【问题描述】:
我对 C# 和 .NET 的东西非常陌生,当我遇到问题时,我一直在尝试一些 HTML 解析和文件下载。 我之前在 .NET Core 中编写过我的代码,它运行良好,然后我意识到我需要使用 System.Windows.Forms 才能访问用户的监视器分辨率,并尝试将我的代码传输到 .NET 框架应用程序以便能够添加 System.Windows.Forms 程序集(显然在 .NET Core 中是不可能的,如果我错了请告诉我??????)。 所以这里是代码:
using System;
using System.Linq;
using HtmlAgilityPack;
using System.Net;
using System.IO;
namespace AmazingSuperAwesomeWallpaperDownloaderAppConsoleApp
{
class Program
{
static void Main(string[] args)
{
Download();
}
static void Download()
{
Console.WriteLine("Please enter the wallpaper page ID: ");
//Asks for user to input the Wallpaper ID and creates the link out of it.
var userInput = "https://SuperSecretWallpaperWebsite.com/Wallpapers/" + Console.ReadLine();
// set the html var to the website link to parse
var html = @userInput;
//gets the website html from the HTTP
HtmlWeb web = new HtmlWeb();
//Creates a var called document to load the wesite in it.
var document = web.Load(html);
var fileName = "Wallpaper.jpg";
//Creates a container, the first div to pass a certain criteria for the container will populate this container
var container = document.DocumentNode.Descendants("div").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "wallpaperContainer");
if (container != null)
{
//Does the same thing as above and looks for the Div with details class and stores it in the title var.
var titleContainer = container.Descendants("div").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "details");
if (title != null)
{
//Finds the first h1 tag that is in title var and stores it in titlevalue
var titleValue = titleContainer.Descendants("h1").FirstOrDefault();
if (titleValue != null)
{
//A var that is set to the innter text of the the h1 that was found in the titleValue var.
var h1TitleValue = titleValue.InnerText;
//Adds file extension to the file name which is the title
fileName = h1TitleValue + ".jpg";
Console.WriteLine("Title: " + h1TitleValue);
}
}
}
//Creates aTag container, the first aTag to pass a certain criteria for the container will populate this container
var aTagContainer = document.DocumentNode.Descendants("a").FirstOrDefault(x => x.Attributes.Contains("class") && x.Attributes["class"].Value == "downloadButton" && x.Attributes.Contains("href"));
if (aTagContainer != null)
{
//Splits the wallpaper URL from the rest of the useless url stuff
var DLink = aTagContainer.Attributes["href"].Value.Split("Url=");
using (var client = new WebClient())
{
//Finds the MyPictures folder for the logged in user and sets it to the wallpaper directory
var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "SuperAwesomeWallpaperDownloadFolder\\");
//Creates the folder in MyPicture folder
System.IO.Directory.CreateDirectory(filePath);
//Downloads the wallpaper file and saves it in the directory
client.DownloadFile(DLink[1], (filePath + fileName));
Console.WriteLine(filePath);
}
Console.WriteLine("Download Link is: " + DLink[1]);
}
}
}
}
在使用 .NET 框架的新控制台应用程序中复制和粘贴代码后,出现错误
var DLink = aTagContainer.Attributes["href"].Value.Split("Url=");
说在 Split 内部它应该是一个 Char 而不是一个字符串!那为什么它可以在 .NET Core 中工作?
client.DownloadFile(DLink[1], (filePath + fileName));
此外,由于某种原因,下载功能似乎不喜欢它的设置方式,尽管在 .NET Core 中运行良好,但无论如何都无法正常工作。
另外我知道我的代码可能写得不太好,我也很高兴听到你对此的批评。 提前谢谢你好心的陌生人????
【问题讨论】:
-
拆分问题很简单,您只需要访问文档并查看重载String.Split Method 就像“并且无法工作” - 这不是技术问题对问题的描述,并且根本没有提供关于实际发生的事情、结果状态以及您期望的信息。关于您的代码,您可能会从 HTML 解析器中获得更强大的解决方案,例如 html-agility-pack
-
感谢您的评论,正如我所提到的,我对这一切都很陌生,并不知道如何解释我的问题。但现在已经修复了。下次我会在我的问题上做得更好。????
标签: c# .net .net-core html-parsing html-agility-pack