C# ile Bir Blog Nasıl Taratılır
Iron WebScraper'ı kullanarak C# veya VB.NET ile Blog içeriğini çıkartalım.
Bu eğitim, WordPress blogunun (veya benzeri) içeriğinin .NET kullanarak nasıl geri yüklenebileceğ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ştururuz ve WebScraper sınıfından miras alırız. Bu durumda, "BlogScraper"dir.
Tüm çıktı ve önbellek dosyalarımızın gidebileceği bir çalışma dizini olarak "\BlogSample\Output\" ayarlıyoruz.
Ardından, önbellek klasörü "WebCache" içinde istenen sayfaları kaydetmek için web önbelleğini etkinleştiriyoruz.
Şimdi bir çözümleme fonksiyonu 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, tüm kategori sayfalarının (Filmler, Bilim, İncelemeler, vb.) linklerini üst menüden alırız.
Daha sonra, bağlantı kategorisine göre uygun bir çözümleme yöntemi seçeriz.
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 bir sayfa taraması gerçekleştirelim:
/// <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, yanıt nesnesini başlık, yazar, tarih, resim, metin gibi ana öğelerine dalarak çözümleyebiliriz.
Daha sonra, sonuçlarımızı Scrape(object, fileName) kullanarak ayrı bir dosyaya kaydederiz.
IronWebscraper kullanımına dair tam eğitim için burayı tıklayın
IronWebscraper ile başlayın
Sıkça Sorulan Sorular
C# ile bir blog web kazıyıcı nasıl oluştururum?
C# dilinde bir blog web kazıyıcı oluşturmak için IronWebScraper kütüphanesini kullanabilirsiniz. Başlamak için WebScraper sınıfını genişleten bir sınıf tanımlayın, bir başlangıç URL'si belirleyin, kazıyıcıyı farklı sayfa türlerini işlemek üzere 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ıma sırasında, Parse yöntemi HTTP yanıtlarını işlemek için gereklidir. Sayfaların içeriğini çözümleyerek veri çıkarmaya, bağlantıları belirlemeye ve blog yazısı veya diğer bölümler gibi sayfa türlerini kategorize etmeye yardımcı olur.
Web kazıma verilerini nasıl verimli bir şekilde yönetebilirim?
IronWebScraper, istenen sayfaları depolamak için önbelleği yapılandırarak ve çıkış dosyaları için bir çalışma dizini ayarlayarak verilerin verimli bir şekilde yönetilmesine olanak tanır. Bu düzenleme, kazınmış verileri takip etmeye ve gereksiz yeniden getirmeyi azaltmaya yardımcı olur.
IronWebScraper, WordPress bloglarının kazınmasına nasıl yardımcı olur?
IronWebScraper, blog yapıları arasında gezinme, gönderi ayrıntılarını çıkartma ve çeşitli sayfa türlerini yönetme araçları sağlayarak WordPress bloglarının kazınmasını basitleştirir. Kütüphaneyi başlık, yazar, tarih, resim ve metin gibi bilgileri almak için gönderileri çözümlemek için kullanabilirsiniz.
IronWebScraper'i hem C# hem de VB.NET için kullanabilir miyim?
Evet, IronWebScraper hem C# hem de VB.NET ile uyumludur ve bu da iki .NET dilinden birini tercih eden geliştiriciler için çok yönlü bir seçenek sunar.
Bir blog içinde farklı türdeki sayfaları nasıl yönetebilirim?
IronWebScraper'da Parse yöntemini geçersiz kılarak bir blog içindeki farklı türdeki sayfaları yönetebilirsiniz. Bu yaklaşım, sayfaları incelemeler ve bilim gibi farklı bölümlere ayırmanıza ve her birine spesifik çözümleme mantığı uygulamanıza olanak tanır.
Birebir yapılandırılmış bir formatta kazınan blog verilerini kaydetmenin bir yolu var mı?
Evet, IronWebScraper kullanarak kazınan blog verilerini JSONL gibi yapılandırılmış bir formatta kaydedebilirsiniz. Bu format, verilerin her bir parçasını satır-satır JSON formatında depolamak için yararlıdır ve daha sonra yönetimini ve işlemeyi kolaylaştırır.
Web kazıyıcım için bir çalışma dizini nasıl ayarlayabilirim?
IronWebScraper'da, çıktı ve önbellek dosyalarının saklanacağı yeri belirtmek için kazıyıcıyı yapılandırarak bir çalışma dizini ayarlayabilirsiniz. Bu, kazınan verilerin verimli bir şekilde düzenlenmesine 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şiklikler, hız sınırlarının yönetilmesi ve kazımayı engelleme önlemleriyle başa çıkma yer alır. IronWebScraper kullanarak hata yönetimi ve günlüğe kaydetme uygulayarak bu sorunları teşhis edebilir ve çözebilirsiniz.
IronWebScraper kullanımı hakkında daha fazla bilgi edinmek için kaynakları nerede bulabilirim?
IronWebScraper kullanımı ile ilgili kaynakları ve eğitimleri, Iron Software web sitesinde web kazıma eğitimi bölümünde detaylı kılavuzlar ve örnekler sağlayan kaynakları bulabilirsiniz.


