使用AngleSharp在C#中解析JavaScript网页

前端之家收集整理的这篇文章主要介绍了使用AngleSharp在C#中解析JavaScript网页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
网页使用 javascript来构建其html所以我需要支持js的html解析器.
我发现了角度锐利,但我不能让它起作用.
  1. using AngleSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace AngleSharpScraping
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. GetMkvToolNix();
  16. Console.ReadKey();
  17. }
  18.  
  19. static async void GetMkvToolNix()
  20. {
  21. // Create a new configuration with javascript interpreter.
  22. var config = new Configuration().WithJavaScript();
  23.  
  24. // Parsing process.
  25. var document = await BrowsingContext.New(config).OpenAsync(Url.Create("http://www.fosshub.com/MKVToolNix.html"));
  26. var link = document.QuerySelector("body > div.container.page-content > div > div.col-sm-9 > article > div.main-dl-Box > p:nth-child(2) > a.dwl-link.xlink").GetAttribute("data");
  27.  
  28. Console.WriteLine(link);
  29. }
  30. }
  31. }

解决方法

AngleSharp是一个文本解析器.如果你想用JS抓取动态网页,你需要一个无头浏览器.

This answer提供了几个选项(至少一个免费和开源:WebKit.NET).

猜你在找的C#相关文章