bal*_*zer 6 javascript c# web-scraping anglesharp
网页使用javascript来构建其html所以我需要支持js的html解析器.
我发现了角度锐利,但我不能让它起作用.
using AngleSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace AngleSharpScraping
{
class Program
{
static void Main(string[] args)
{
GetMkvToolNix();
Console.ReadKey();
}
static async void GetMkvToolNix()
{
// Create a new configuration with javascript interpreter.
var config = new Configuration().WithJavaScript();
// Parsing process.
var document = await BrowsingContext.New(config).OpenAsync(Url.Create("http://www.fosshub.com/MKVToolNix.html"));
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");
Console.WriteLine(link);
}
}
}
Run Code Online (Sandbox Code Playgroud)
AngleSharp 仅提供 HTML 和 CSS 解析器。然而,AngleSharp 可以通过 JavaScript 功能进行扩展。现在,您使用的包(AngleSharp.Scripting.JavaScript)是实验性的,或多或少是一个概念证明。
页面上的 JavaScript 文件对于实验支持来说仍然太复杂。我努力尽快启用对此类场景的支持,但现在我想说 WebKit.NET 可能是解决您的问题的最佳选择。
另一种可能的解决方案可能是使用Selenium的 C# 驱动程序。
与整个 JavaScript 主题无关:如果您想加载外部资源,您需要提供适当的 (http) 请求者。最简单的方法是使用默认方法:
var config = new Configuration().WithDefaultLoader();
var document = await BrowsingContext.New(config).OpenAsync("http://www.fosshub.com/MKVToolNix.html");
// ...
Run Code Online (Sandbox Code Playgroud)
在此设置中,会加载外部文档,但不会加载其他资源(例如图像、脚本等)。
AngleSharp 是一个文本解析器。如果你想用 JS 抓取动态网页,你需要一个无头浏览器。
这个答案提供了几个选项(至少一个免费且开源的:WebKit.NET)。
| 归档时间: |
|
| 查看次数: |
7577 次 |
| 最近记录: |