c# - C# 控制台应用程序代码在等待后不执行
问题描述
我正在尝试制作一个网络爬虫,在其中从 html 文件中获取 css/js/images 的所有下载链接。
问题
第一个断点确实命中,但第二个断点在点击“继续”之后没有命中。
我正在谈论的代码:
private static async void GetHtml(string url, string downloadDir)
{
//Get html data, create and load htmldocument
HttpClient httpClient = new HttpClient();
//This code gets executed
var html = await httpClient.GetStringAsync(url);
//This code not
Console.ReadLine();
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//Get all css download urls
var linkUrl = htmlDocument.DocumentNode.Descendants("link")
.Where(node => node.GetAttributeValue("type", "")
.Equals("text/css"))
.Select(node=>node.GetAttributeValue("href",""))
.ToList();
//Downloading css, js, images and source code
using (var client = new WebClient())
{
for (var i = 0; i <scriptUrl.Count; i++)
{
Uri uri = new Uri(scriptUrl[i]);
client.DownloadFile(uri,
downloadDir + @"\js\" + uri.Segments.Last());
}
}
编辑
我从这里调用 getHtml 方法:
private static void Start()
{
//Create a list that will hold the names of all the subpages
List<string> subpagesList = new List<string>();
//Ask user for url and asign that to var url, also add the url to the url list
Console.WriteLine("Geef url van de website:");
string url = "https://www.hethwc.nl";
//Ask user for download directory and assign that to var downloadDir
Console.WriteLine("Geef locatie voor download:");
var downloadDir = @"C:\Users\Daniel\Google Drive\Almere\C# II\Download tests\hethwc\";
//Download and save the index file
var htmlSource = new System.Net.WebClient().DownloadString(url);
System.IO.File.WriteAllText(@"C:\Users\Daniel\Google Drive\Almere\C# II\Download tests\hethwc\index.html", htmlSource);
// Creating directories
string jsDirectory = System.IO.Path.Combine(downloadDir, "js");
string cssDirectory = System.IO.Path.Combine(downloadDir, "css");
string imagesDirectory = System.IO.Path.Combine(downloadDir, "images");
System.IO.Directory.CreateDirectory(jsDirectory);
System.IO.Directory.CreateDirectory(cssDirectory);
System.IO.Directory.CreateDirectory(imagesDirectory);
GetHtml("https://www.hethwc.nu", downloadDir);
}
解决方案
你怎么叫GetHtml
?大概是来自同步Main
方法,并且您没有任何其他非工作线程在运行(因为您的主线程已退出):进程将终止。就像是:
static void Main() {
GetHtml();
}
GetHtml
以上将在返回后立即终止过程,Main
方法结束,这将是第一个不完整的await
点。
在当前的 C# 版本(C# 7.1 及更高版本)中,您可以创建一个方法,只要您更改为 return async Task Main()
,它将允许您正确地使用await
您的方法:GetHtml
GetHtml
Task
async static Task Main() {
await GetHtml();
}
推荐阅读
- javascript - 圆环图内的粗体文本 (chart.js)
- android - 如何在 DataBinding Android 的 BindingAdapter 方法中提供默认值
- kotlin - Kotlin - 委托链
- ruby-on-rails - 无法使用 Date.commercial 方法找到第 53 周的开始
- php - Laravel foreach 不工作
- vega - Vega lite/Altair 解析日期以季度指定
- ios - 不存在的约束xcode的约束冲突错误
- c# - "The handshake failed due to an unexpected packet format" with SmtpClient
- angular - AngularFire - Firestore - Check if a document exists whilst performing queries
- windows - 如何使用 Visual Basic 确定 Windows 显示语言?