使用AngleSharp编写程序来分析来自一个网站的数据。不幸的是,我没有找到任何文档,这使得理解变得非常困难。
QuerySelectorAll获得唯一的链接?我现在用<a ...>...</a>和Name of article来完成所有的事情。<a href="http://kinnisvaraportaal-kv-ee.postimees.ee/muua-odra-tanaval-kesklinnas-valmiv-suur-ja-avar-k-2904668.html?nr=1&search_key=69ec78d9b1758eb34c58cf8088c96d10" class="object-title-a text-truncate">1. Name of artucle</a>
我现在使用的方法是:
var items = document.QuerySelectorAll("a").Where(item => item.ClassName != null && item.ClassName.Contains("object-title-a text-truncate"));.<th class="strong">Room</th> <td>4</td>.
发布于 2017-05-17 16:25:57
关于你的第一个问题。下面是一个可以提取链接地址的示例。这是另一个与Stackoveflow相关的帖子的链接。
var source = @"<a href='http://kinnisvaraportaal-kv-ee.postimees.ee/muua-odra-tanaval-kesklinnas-valmiv-suur-ja-avar-k-2904668.html?nr=1&search_key=69ec78d9b1758eb34c58cf8088c96d10' class='object-title-a text-truncate'>1. Name of artucle</a>";
var parser = new HtmlParser();
var doc = parser.Parse(source);
var selector = "a";
var menuItems = doc.QuerySelectorAll(selector).OfType<IHtmlAnchorElement>();
foreach (var i in menuItems)
{
Console.WriteLine(i.Href);
}对于第二个问题,您可以查看文档上的示例,下面是链接,下面是代码示例:
// Setup the configuration to support document loading
var config = Configuration.Default.WithDefaultLoader();
// Load the names of all The Big Bang Theory episodes from Wikipedia
var address = "https://en.wikipedia.org/wiki/List_of_The_Big_Bang_Theory_episodes";
// Asynchronously get the document in a new context using the configuration
var document = await BrowsingContext.New(config).OpenAsync(address);
// This CSS selector gets the desired content
var cellSelector = "tr.vevent td:nth-child(3)";
// Perform the query to get all cells with the content
var cells = document.QuerySelectorAll(cellSelector);
// We are only interested in the text - select it with LINQ
var titles = cells.Select(m => m.TextContent);https://stackoverflow.com/questions/44022875
复制相似问题