- Mesaj
- 107
- Beğeni
- 81
- Puan
- 351
- Ticaret Puanı
- 0
C#:
using HtmlAgilityPack;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;
using System.Net;
namespace WebScraper
{
class Program
{
static async Task Main(String[] args)
{
String url = "https://tr.wikipedia.org/wiki/Pizza";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36");
var htmlDocument = new HtmlDocument();
try
{
var html = await httpClient.GetStringAsync(url);
htmlDocument.LoadHtml(html);
}
catch (HttpRequestException e)
{
Console.WriteLine($"İstek hatası: {e.Message}");
return;
}
// --- 1. Başlığı Almak ---
var titleElement = htmlDocument.DocumentNode.SelectSingleNode("//h1[@id='firstHeading']");
if (titleElement != null)
{
// HtmlDecode ile " gibi kodları düzeltiyoruz
var title = WebUtility.HtmlDecode(titleElement.InnerText).Trim();
Console.WriteLine("Sayfa Başlığı: " + title);
}
else
{
Console.WriteLine("Başlık elementi bulunamadı.");
}
// --- 2. İlk Paragrafı Almak ---
var paragraphElement = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='mw-content-text']//p[1]");
if (paragraphElement != null)
{
foreach (var node in paragraphElement.SelectNodes(".//sup"))
{
node.Remove(); // [1], [2] gibi referansları kaldır
}
var paragraph = WebUtility.HtmlDecode(paragraphElement.InnerText).Trim();
Console.WriteLine("İlk Paragraf: " + paragraph);
}
else
{
Console.WriteLine("Paragraf elementi bulunamadı.");
}
// --- 3. BÖLÜM: "Pizza Çeşitleri" Tablosu ---
Console.WriteLine("\n--- 🍕 Pizza Çeşitleri Tablosu ---");
// "Pizza çeşitleri" başlıklı (caption) tabloyu bul
var table1 = htmlDocument.DocumentNode.SelectSingleNode("//table[caption[contains(., 'Pizza çeşitleri')]]");
if (table1 != null)
{
var rows = table1.SelectNodes(".//tbody/tr");
if (rows != null)
{
foreach (var row in rows)
{
var cells = row.SelectNodes(".//td");
if (cells != null && cells.Count > 1) // En az 2 hücreli satırları al
{
var pizzaName = WebUtility.HtmlDecode(cells[1].InnerText).Trim();
var ingredients = WebUtility.HtmlDecode(cells[2].InnerText).Trim();
Console.WriteLine($"Çeşit: {pizzaName} -> Malzemeler: {ingredients}");
}
}
}
}
else
{
Console.WriteLine("Pizza çeşitleri tablosu bulunamadı.");
}
// --- 4. YENİ BÖLÜM: "Pizza Stilleri" Tablosu ---
Console.WriteLine("\n--- 🍕 Pizza Stilleri Tablosu ---");
// "Pizza stilleri" başlıklı (caption) tabloyu bul
var table2 = htmlDocument.DocumentNode.SelectSingleNode("//table[caption[contains(., 'Pizza stilleri')]]");
if (table2 != null)
{
var rows = table2.SelectNodes(".//tbody/tr");
if (rows != null)
{
foreach (var row in rows)
{
var cells = row.SelectNodes(".//td");
if (cells != null && cells.Count > 1) // En az 2 hücreli satırları al
{
var styleName = WebUtility.HtmlDecode(cells[1].InnerText).Trim();
var description = WebUtility.HtmlDecode(cells[2].InnerText).Trim();
Console.WriteLine($"Stil: {styleName} -> Açıklama: {description}");
}
}
}
}
else
{
Console.WriteLine("Pizza stilleri tablosu bulunamadı.");
}
Console.ReadLine();
}
}
}
mamma mia <3