0Pricing
Web Scraping & Bots · Ders

BeautifulSoup ile Veri Çıkarma

HTML belgelerini ayrıştırmak ve etiketleri, sınıfları ve kimlikleri kullanarak belirli veri noktalarını çıkarmak için BeautifulSoup'u kullanın.

BeautifulSoup ile Veri Çıkarma, CoddyKit'te ücretsiz bir Web Scraping & Bots dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Web Scraping & Bots öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Web Scraping & Bots kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

BeautifulSoup: Your HTML Navigator

You've learned to fetch web page content using requests. But that content is just a big string of HTML!

BeautifulSoup is a Python library that helps you parse (understand) that HTML. It turns the raw HTML into a tree-like structure, making it easy to navigate and extract specific data.

Why Use a Parser?

Imagine trying to find a specific sentence in a book without page numbers or chapters. That's like trying to extract data from raw HTML strings!

BeautifulSoup gives structure to the HTML, allowing you to easily locate elements like headings, paragraphs, or links using their tags, classes, or IDs.

Creating a BeautifulSoup Object

First, you need to import the library and create a BeautifulSoup object. You pass your HTML content and specify a parser, usually 'html.parser'.

from bs4 import BeautifulSoup

html_doc = """<html><head><title>My Page</title></head><body><h1>Hello</h1><p>World</p></body></html>"""

soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())

Finding Elements by Tag

The simplest way to find elements is by their HTML tag name. You can use soup.find() to get the first matching tag or soup.find_all() to get all matching tags.

  • find('tag_name'): Returns a single Tag object or None.
  • find_all('tag_name'): Returns a list of Tag objects.

Code: Finding Tags

Let's find the <title> and all <p> tags in our example HTML.

from bs4 import BeautifulSoup

html_doc = """<html><head><title>My Page</title></head><body><h1>Welcome!</h1><p class='intro'>This is an intro.</p><p>Another paragraph.</p></body></html>"""

soup = BeautifulSoup(html_doc, 'html.parser')

# Find the first title tag
title_tag = soup.find('title')
print(f"Title tag: {title_tag}")

# Find all paragraph tags
paragraph_tags = soup.find_all('p')
print(f"Paragraph tags found: {len(paragraph_tags)}")
for p in paragraph_tags:
    print(f" - {p.get_text()}")

Extracting Text and Attributes

Once you have a Tag object, you can easily get its text content or attribute values.

  • To get text: Use the .get_text() method.
  • To get attributes: Access them like a dictionary, e.g., tag['href'] or tag.get('href').

Code: Getting Text and Attributes

Let's extract the text from the <h1> and an attribute from a link.

from bs4 import BeautifulSoup

html_doc = """<html><body><h1 id='main-h1'>Hello CoddyKit</h1><a href='https://example.com' class='btn'>Visit Example</a></body></html>"""

soup = BeautifulSoup(html_doc, 'html.parser')

h1_tag = soup.find('h1')
print(f"H1 Text: {h1_tag.get_text()}")

link_tag = soup.find('a')
print(f"Link Text: {link_tag.get_text()}")
print(f"Link Href: {link_tag['href']}")
print(f"Link Class: {link_tag['class']}")

Finding Elements by Class

Web pages often use CSS classes to style and identify elements. BeautifulSoup lets you search for elements with a specific class using the class_ argument.

Why class_ and not class? Because class is a reserved keyword in Python!

Code: Finding by Class

Here's how to find paragraphs specifically marked with class="intro".

from bs4 import BeautifulSoup

html_doc = """<html><body><p class='intro'>Welcome message.</p><p>Normal text.</p><p class='intro'>Another intro.</p></body></html>"""

soup = BeautifulSoup(html_doc, 'html.parser')

intro_paragraphs = soup.find_all('p', class_='intro')

for p in intro_paragraphs:
    print(f"Found intro: {p.get_text()}")

Finding Elements by ID

HTML IDs are unique identifiers for elements within a page. You can target a specific element using its ID with the id argument.

Since IDs should be unique per page, find() is often sufficient when searching by ID.

Quick Check: Element Selection

Given the HTML snippet below, how would you extract the text 'Item 2'?

<div id="items">
  <span class="item-name">Item 1</span>
  <span class="item-name">Item 2</span>
  <span class="item-name">Item 3</span>
</div>

Recap: BeautifulSoup Basics

Great job! You've learned the fundamentals of extracting data using BeautifulSoup:

  • Initializing a BeautifulSoup object from HTML.
  • Finding elements by tag name using find() and find_all().
  • Extracting text content with .get_text() and attributes with tag['attr'].
  • Targeting elements precisely using CSS classes with class_.
  • Locating unique elements with IDs using id.

These methods are your building blocks for parsing web pages efficiently!

Sıkça Sorulan Sorular

“BeautifulSoup ile Veri Çıkarma” dersi ücretsiz mi?

Evet — “BeautifulSoup ile Veri Çıkarma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Web Scraping & Bots kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Web Scraping & Bots kursu toplamda 4 dersten oluşur.

“BeautifulSoup ile Veri Çıkarma” dersinde ne öğreneceğim?

HTML belgelerini ayrıştırmak ve etiketleri, sınıfları ve kimlikleri kullanarak belirli veri noktalarını çıkarmak için BeautifulSoup'u kullanın. Web Scraping & Bots ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Web Scraping & Bots öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Web Scraping & Bots, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“BeautifulSoup ile Veri Çıkarma” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Web Scraping & Bots dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Web Scraping & Bots dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Ortamınızı Kurma
  2. URL'ler İçin Requests Kullanımı
  3. BeautifulSoup ile Veri Çıkarma
  4. CSS Seçicileriyle DOM'da Gezinme
← Web Scraping & Bots Sayfasına Dön