You can create a high-level spider by inheriting from Spider and defining an entity class. Use attributes to configure data extraction, selectors, and formatting:
[Schema(collection, table)]: Defines the storage schema.[EntitySelector(Expression, Type)]: Defines the XPath/CSS selector for the main entity container.[ValueSelector(Expression, Type)]: Defines how to extract specific fields.[GlobalValueSelector(Expression, Name, Type)]: Extracts values to be used globally across all entities in a page.[FollowRequestSelector(Expressions)]: Defines selectors for finding links to follow.[ReplaceFormatter(NewValue, OldValue)]: Formats string values by replacing content.[TrimFormatter]: Trims whitespace from extracted strings.
Example implementation:
[DisplayName("博客园爬虫")]
public class EntitySpider(
IOptions<SpiderOptions> options,
DependenceServices services,
ILogger<Spider> logger)
: Spider(options, services, logger)
{
public static async Task RunAsync()
{
var builder = Builder.CreateDefaultBuilder<EntitySpider>(options =>
{
options.Speed = 1;
});
builder.UseSerilog();
builder.IgnoreServerCertificateError();
await builder.Build().RunAsync();
}
protected override async Task InitializeAsync(CancellationToken stoppingToken = default)
{
AddDataFlow<DataParser<CnblogsEntry>>();
AddDataFlow(GetDefaultStorage);
await AddRequestsAsync(
new Request("https://news.cnblogs.com/n/page/1", new Dictionary<string, object> { { "网站", "博客园" } }));
}
[Schema("cnblogs", "news")]
[EntitySelector(Expression = ".//div[@class='news_block']", Type = SelectorType.XPath)]
[GlobalValueSelector(Expression = ".//a[@class='current']", Name = "类别", Type = SelectorType.XPath)]
[GlobalValueSelector(Expression = "//title", Name = "Title", Type = SelectorType.XPath)]
[FollowRequestSelector(Expressions = ["//div[@class='pager']"])]
public class CnblogsEntry : EntityBase<CnblogsEntry>
{
protected override void Configure()
{
HasIndex(x => x.Title);
HasIndex(x => new { x.WebSite, x.Guid }, true);
}
public int Id { get; set; }
[Required]
[StringLength(200)]
[ValueSelector(Expression = "类别", Type = SelectorType.Environment)]
public string Category { get; set; }
[Required]
[StringLength(200)]
[ValueSelector(Expression = "网站", Type = SelectorType.Environment)]
public string WebSite { get; set; }
[StringLength(200)]
[ValueSelector(Expression = "Title", Type = SelectorType.Environment)]
[ReplaceFormatter(NewValue = "", OldValue = " - 博客园")]
public string Title { get; set; }
[StringLength(40)]
[ValueSelector(Expression = "GUID", Type = SelectorType.Environment)]
public string Guid { get; set; }
[ValueSelector(Expression = ".//h2[@class='news_entry']/a")]
public string News { get; set; }
[ValueSelector(Expression = ".//h2[@class='news_entry']/a/@href")]
public string Url { get; set; }
[ValueSelector(Expression = ".//div[@class='entry_summary']")]
[TrimFormatter]
public string PlainText { get; set; }
[ValueSelector(Expression = "DATETIME", Type = SelectorType.Environment)]
public DateTime CreationTime { get; set; }
}
}