C# ile Bir Blog Nasıl Kazınır
C# veya VB.NET kullanarak Blog içeriği çıkarmak için Iron WebScraper'ı kullanalım.
Bu eğitim, bir WordPress blogunun (veya benzeri) içeriğinin .NET kullanılarak nasıl geri kazınabileceğini gösterir.
// Define a class that extends WebScraper from IronWebScraper
public class BlogScraper : WebScraper
{
/// <summary>
/// Override this method to initialize your web-scraper.
/// Set at least one start URL and configure domain or URL patterns.
/// </summary>
public override void Init()
{
// Set your license key for IronWebScraper
License.LicenseKey = "YourLicenseKey";
// Enable logging for all actions
this.LoggingLevel = WebScraper.LogLevel.All;
// Set a directory to store output and cache files
this.WorkingDirectory = AppSetting.GetAppRoot() + @"\BlogSample\Output\";
// Enable caching with a specific duration
EnableWebCache(new TimeSpan(1, 30, 30));
// Request the start URL and specify the response handler
this.Request("http://blogSite.com/", Parse);
}
}
// Define a class that extends WebScraper from IronWebScraper
public class BlogScraper : WebScraper
{
/// <summary>
/// Override this method to initialize your web-scraper.
/// Set at least one start URL and configure domain or URL patterns.
/// </summary>
public override void Init()
{
// Set your license key for IronWebScraper
License.LicenseKey = "YourLicenseKey";
// Enable logging for all actions
this.LoggingLevel = WebScraper.LogLevel.All;
// Set a directory to store output and cache files
this.WorkingDirectory = AppSetting.GetAppRoot() + @"\BlogSample\Output\";
// Enable caching with a specific duration
EnableWebCache(new TimeSpan(1, 30, 30));
// Request the start URL and specify the response handler
this.Request("http://blogSite.com/", Parse);
}
}
' Define a class that extends WebScraper from IronWebScraper
Public Class BlogScraper
Inherits WebScraper
''' <summary>
''' Override this method to initialize your web-scraper.
''' Set at least one start URL and configure domain or URL patterns.
''' </summary>
Public Overrides Sub Init()
' Set your license key for IronWebScraper
License.LicenseKey = "YourLicenseKey"
' Enable logging for all actions
Me.LoggingLevel = WebScraper.LogLevel.All
' Set a directory to store output and cache files
Me.WorkingDirectory = AppSetting.GetAppRoot() & "\BlogSample\Output\"
' Enable caching with a specific duration
EnableWebCache(New TimeSpan(1, 30, 30))
' Request the start URL and specify the response handler
Me.Request("http://blogSite.com/", Parse)
End Sub
End Class
Her zamanki gibi, bir Scraper oluşturuyoruz ve WebScraper sınıfından miras alıyoruz. Bu durumda, "BlogScraper" şeklindedir.
"\BlogSample\Output\" çalışma dizini ayarlıyoruz, böylece tüm çıktı ve önbellek dosyalarımız oraya gidebilir.
Daha sonra, talep edilen sayfaları "WebCache" adlı önbellek klasöründe saklamak için web önbelleğini etkinleştiriyoruz.
Şimdi bir Parse işlevi yazalım:
/// <summary>
/// Override this method to handle the Http Response for your web scraper.
/// Add additional methods if you handle multiple page types.
/// </summary>
/// <param name="response">The HTTP Response object to parse.</param>
public override void Parse(Response response)
{
// Iterate over each link found in the section navigation
foreach (var link in response.Css("div.section-nav > ul > li > a"))
{
switch(link.TextContentClean)
{
case "Reviews":
{
// Handle reviews case
}
break;
case "Science":
{
// Handle science case
}
break;
default:
{
// Save the link title to a file
Scrape(new ScrapedData() { { "Title", link.TextContentClean } }, "BlogScraper.Jsonl");
}
break;
}
}
}
/// <summary>
/// Override this method to handle the Http Response for your web scraper.
/// Add additional methods if you handle multiple page types.
/// </summary>
/// <param name="response">The HTTP Response object to parse.</param>
public override void Parse(Response response)
{
// Iterate over each link found in the section navigation
foreach (var link in response.Css("div.section-nav > ul > li > a"))
{
switch(link.TextContentClean)
{
case "Reviews":
{
// Handle reviews case
}
break;
case "Science":
{
// Handle science case
}
break;
default:
{
// Save the link title to a file
Scrape(new ScrapedData() { { "Title", link.TextContentClean } }, "BlogScraper.Jsonl");
}
break;
}
}
}
''' <summary>
''' Override this method to handle the Http Response for your web scraper.
''' Add additional methods if you handle multiple page types.
''' </summary>
''' <param name="response">The HTTP Response object to parse.</param>
Public Overrides Sub Parse(ByVal response As Response)
' Iterate over each link found in the section navigation
For Each link In response.Css("div.section-nav > ul > li > a")
Select Case link.TextContentClean
Case "Reviews"
' Handle reviews case
Case "Science"
' Handle science case
Case Else
' Save the link title to a file
Scrape(New ScrapedData() From {
{ "Title", link.TextContentClean }
},
"BlogScraper.Jsonl")
End Select
Next link
End Sub
Parse yöntemi içinde, üst menüden kategori sayfalarına (Filmler, Bilim, İncelemeler vb.) giden tüm bağlantıları alırız.
Daha sonra bağlantı kategorisine göre uygun bir ayıklama yöntemi seçiyoruz.
Bilim Sayfası için nesne modelimizi hazırlayalım:
/// <summary>
/// Represents a model for Science Page
/// </summary>
public class ScienceModel
{
/// <summary>
/// Gets or sets the title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets the author.
/// </summary>
public string Author { get; set; }
/// <summary>
/// Gets or sets the date.
/// </summary>
public string Date { get; set; }
/// <summary>
/// Gets or sets the image.
/// </summary>
public string Image { get; set; }
/// <summary>
/// Gets or sets the text.
/// </summary>
public string Text { get; set; }
}
/// <summary>
/// Represents a model for Science Page
/// </summary>
public class ScienceModel
{
/// <summary>
/// Gets or sets the title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets the author.
/// </summary>
public string Author { get; set; }
/// <summary>
/// Gets or sets the date.
/// </summary>
public string Date { get; set; }
/// <summary>
/// Gets or sets the image.
/// </summary>
public string Image { get; set; }
/// <summary>
/// Gets or sets the text.
/// </summary>
public string Text { get; set; }
}
''' <summary>
''' Represents a model for Science Page
''' </summary>
Public Class ScienceModel
''' <summary>
''' Gets or sets the title.
''' </summary>
Public Property Title() As String
''' <summary>
''' Gets or sets the author.
''' </summary>
Public Property Author() As String
''' <summary>
''' Gets or sets the date.
''' </summary>
Public Property [Date]() As String
''' <summary>
''' Gets or sets the image.
''' </summary>
Public Property Image() As String
''' <summary>
''' Gets or sets the text.
''' </summary>
Public Property Text() As String
End Class
Şimdi tek sayfa kazımasını uygulayalım:
/// <summary>
/// Parses the reviews from the response.
/// </summary>
/// <param name="response">The HTTP Response object.</param>
public void ParseReviews(Response response)
{
// A list to hold Science models
var scienceList = new List<ScienceModel>();
foreach (var postBox in response.Css("section.main > div > div.post-list"))
{
var item = new ScienceModel
{
Title = postBox.Css("h1.headline > a")[0].TextContentClean,
Author = postBox.Css("div.author > a")[0].TextContentClean,
Date = postBox.Css("div.time > a")[0].TextContentClean,
Image = postBox.Css("div.image-wrapper.default-state > img")[0].Attributes["src"],
Text = postBox.Css("div.summary > p")[0].TextContentClean
};
scienceList.Add(item);
}
// Save the science list to a JSONL file
Scrape(scienceList, "BlogScience.Jsonl");
}
/// <summary>
/// Parses the reviews from the response.
/// </summary>
/// <param name="response">The HTTP Response object.</param>
public void ParseReviews(Response response)
{
// A list to hold Science models
var scienceList = new List<ScienceModel>();
foreach (var postBox in response.Css("section.main > div > div.post-list"))
{
var item = new ScienceModel
{
Title = postBox.Css("h1.headline > a")[0].TextContentClean,
Author = postBox.Css("div.author > a")[0].TextContentClean,
Date = postBox.Css("div.time > a")[0].TextContentClean,
Image = postBox.Css("div.image-wrapper.default-state > img")[0].Attributes["src"],
Text = postBox.Css("div.summary > p")[0].TextContentClean
};
scienceList.Add(item);
}
// Save the science list to a JSONL file
Scrape(scienceList, "BlogScience.Jsonl");
}
''' <summary>
''' Parses the reviews from the response.
''' </summary>
''' <param name="response">The HTTP Response object.</param>
Public Sub ParseReviews(ByVal response As Response)
' A list to hold Science models
Dim scienceList = New List(Of ScienceModel)()
For Each postBox In response.Css("section.main > div > div.post-list")
Dim item = New ScienceModel With {
.Title = postBox.Css("h1.headline > a")(0).TextContentClean,
.Author = postBox.Css("div.author > a")(0).TextContentClean,
.Date = postBox.Css("div.time > a")(0).TextContentClean,
.Image = postBox.Css("div.image-wrapper.default-state > img")(0).Attributes("src"),
.Text = postBox.Css("div.summary > p")(0).TextContentClean
}
scienceList.Add(item)
Next postBox
' Save the science list to a JSONL file
Scrape(scienceList, "BlogScience.Jsonl")
End Sub
Modelimizi oluşturduktan sonra, Response nesnesini ayrıştırarak ana öğelerine (başlık, yazar, tarih, resim, metin) inebiliriz.
Ardından, sonuçlarımızı Scrape(object, fileName) kullanarak ayrı bir dosyaya kaydederiz.
IronWebScraper kullanımı için tam eğitimi görmek için buraya tıklayın
IronWebScraper ile Başlayın
Sıkça Sorulan Sorular
C#'ta bir blog web kazıyıcıyı nasıl oluştururum?
C#'ta bir blog web kazıyıcı oluşturmak için IronWebScraper kütüphanesini kullanabilirsiniz. Öncelikle, WebScraper sınıfını genişleten bir sınıf tanımlayın, bir başlangıç URL'si ayarlayın, kazıyıcıyı farklı sayfa türlerini işlemek için yapılandırın ve HTTP yanıtlarından istenen bilgileri çıkarmak için Parse yöntemini kullanın.
web kazımada Parse yönteminin işlevi nedir?
IronWebScraper ile web kazımada, Parse yöntemi, HTTP yanıtlarını işlemede esastır. Sayfaların içeriğini ayrıştırarak verileri çıkarmaya, bağlantıları tanımlamaya ve blog gönderileri veya diğer bölümler gibi sayfa türlerini kategorize etmeye yardımcı olur.
web kazıma verilerini verimli bir şekilde nasıl yönetirim?
IronWebScraper, talep edilen sayfaları saklamak için önbelleği yapılandırarak ve çıkış dosyaları için bir çalışma dizini ayarlayarak verimli veri yönetimi sağlar. Bu organizasyon, kazınan verileri takip etmeye yardımcı olur ve sayfaların gereksiz şekilde yeniden getirilmesini azaltır.
IronWebScraper, WordPress bloglarını kazımada nasıl yardımcı olur?
IronWebScraper, blog yapılarında gezinme, gönderi ayrıntılarını çıkarma ve çeşitli sayfa türlerini işleme araçlarını sağlayarak WordPress bloglarının kazınmasını kolaylaştırır. Kitaplığı, başlık, yazar, tarih, resim ve metin gibi bilgileri çıkarmak için gönderileri ayrıştırmak için kullanabilirsiniz.
IronWebScraper'ı hem C# hem de VB.NET için kullanabilir miyim?
Evet, IronWebScraper hem C# hem de VB.NET ile uyumludur, bu da onu bu .NET dillerinden birini tercih eden geliştiriciler için çok yönlü bir seçim yapar.
bir blog içinde farklı sayfa türlerini nasıl ele alırım?
IronWebScraper'da Parse yöntemini geçersiz kılarak bir blog içindeki farklı sayfa türlerini ele alabilirsiniz. Bu yaklaşım, sayfaları İncelemeler ve Bilim gibi farklı bölümlere kategorize etmenize ve her birine özel ayrıştırma mantığı uygulamanıza olanak tanır.
kazılan blog verilerini yapılandırılmış bir formatta kaydetmenin bir yolu var mı?
Evet, IronWebScraper kullanarak kazılan blog verilerini JSONL gibi yapılandırılmış bir formatta kaydedebilirsiniz. Bu format, her veri parçasını satır satır JSON formatında saklamak için uygundur, bu da daha sonra yönetmeyi ve işlemeyi kolaylaştırır.
benim için bir web kazıyıcısı için bir çalışma dizinini nasıl ayarlayabilirim?
IronWebScraper'da, kazıyıcıyı çıkış ve önbellek dosyalarının saklanacağı yeri belirtmek üzere yapılandırarak bir çalışma dizinini ayarlayabilirsiniz. Bu, kazılan verilerin daha da verimli bir şekilde organize edilmesine yardımcı olur.
web kazımada yaygın sorun giderme senaryoları nelerdir?
Web kazımada yaygın sorun giderme senaryoları arasında web sitesi yapısındaki değişikliklerin ele alınması, oran limitlerinin yönetilmesi ve anti-kazıma önlemleri ile başa çıkılması yer alır. IronWebScraper kullanarak, bu sorunları teşhis etmek ve çözmek için hata ayıklama ve günlüğe kaydetme uygulayabilirsiniz.
IronWebScraper kullanımı hakkında daha fazla bilgi öğrenmek için kaynakları nerede bulabilirim?
IronWebScraper kullanımı hakkında kaynaklar ve eğitim materyalleri, web kazıma eğitimleri bölümü altında detaylı rehberler ve örnekler sunan Iron Software web sitesinde bulunabilir.


