• 13-09-2024, 17:38:55
    #1
    Bu konuda, Python kullanarak Hepsiburada üzerindeki bir ürünün detaylarını nasıl çekebileceğinizi gösteren bir örnek kodu inceleyeceğiz. Web scraping, belirli bir web sitesinden veri çekme işlemi olarak tanımlanır ve bu örnekte Hepsiburada'nın bir ürün sayfasından JSON formatındaki veriyi nasıl çekebileceğinizi göreceksiniz.

    1. Kütüphaneler:


    Kullanacağımız Python kütüphaneleri:
    • requests: HTTP istekleri göndermek için.
    • BeautifulSoup: HTML içeriğini ayrıştırmak için.
    • json: JSON verilerini işlemek için.

    2. Kod Açıklaması:


    Aşağıda verilen kod, Hepsiburada'dan belirli bir ürünün bilgilerini çekmek için yazılmıştır:

    import requests
    from bs4 import BeautifulSoup
    import json
    
    # URL ve headers ayarları
    url = 'https://www.hepsiburada.com/xiaomi-smart-air-purifier-elite-filtre-p-hbcv00005ctu0x'
    headers = {
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'tr,en-US;q=0.9,en;q=0.8,tr-TR;q=0.7',
        'cache-control': 'max-age=0',
        'referer': 'https://www.hepsiburada.com/',
        'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
        'sec-ch-ua-mobile': '?1',
        'sec-ch-ua-platform': '"Android"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36',
    }
    
    # HTTP isteği gönder
    response = requests.get(url, headers=headers)
    html_content = response.text
    
    # BeautifulSoup ile HTML ayrıştır
    soup = BeautifulSoup(html_content, 'html.parser')
    
    # Script içeriğini bulma
    script_tags = soup.find_all('script', type='application/ld+json')
    
    # Script içeriğini kontrol etme
    if not script_tags:
        print("JSON içeren script etiketi bulunamadı.")
    else:
        script_data = script_tags[0].string  # İlk JSON script içeriğini al
        print("Script İçeriği:")
        #print(script_data)  # Script içeriğini yazdırarak kontrol edin
        
        # JSON verisini ayrıştırma
        try:
            json_data = json.loads(script_data)
            if isinstance(json_data, dict) and '@graph' in json_data:
                # Ürün bilgilerini al
                product_info = next((item for item in json_data.get('@graph', []) if item.get('@type') == 'Product'), {})
                
                # Bilgileri yazdır
                print("\nÜrün Bilgileri:")
                print(f"Ürün Adı: {product_info.get('name', 'Bilgi Yok')}")
                print(f"Açıklama: {product_info.get('description', 'Bilgi Yok')}")
                print(f"Kategori: {product_info.get('category', 'Bilgi Yok')}")
                print(f"SKU (Stok Kodu): {product_info.get('sku', 'Bilgi Yok')}")
                print(f"GTIN (Global Ticaret Ürün Numarası): {product_info.get('gtin', 'Bilgi Yok')}")
                print(f"Marka: {product_info.get('brand', {}).get('name', 'Bilgi Yok')}")
                print(f"Fiyat: {product_info.get('offers', {}).get('price', 'Bilgi Yok')} {product_info.get('offers', {}).get('priceCurrency', 'TRY')}")
                print(f"Stok Durumu: {product_info.get('offers', {}).get('availability', 'Bilgi Yok')}")
                print(f"Resimler: {', '.join(product_info.get('image', []))}")
                print(f"Satıcı: {product_info.get('offers', {}).get('seller', {}).get('name', 'Bilgi Yok')}")
                print(f"İade Politikası Linki: {product_info.get('hasMerchantReturnPolicy', {}).get('merchantReturnLink', 'Bilgi Yok')}")
                print(f"İade Günleri: {product_info.get('hasMerchantReturnPolicy', {}).get('merchantReturnDays', 'Bilgi Yok')}")
            else:
                print("JSON verisi beklenen formatta değil.")
        except json.JSONDecodeError as e:
            print(f"JSON ayrıştırma hatası: {e}")

    3. Kodun Açıklaması:

    • URL ve Headers: İstek göndereceğimiz URL ve HTTP başlıkları ayarlanır.
    • HTTP İsteği Gönderme: requests.get ile belirlenen URL'ye istek gönderilir.
    • HTML İçeriği Ayrıştırma: BeautifulSoup kullanılarak HTML içeriği ayrıştırılır.
    • JSON Script Bulma: BeautifulSoup ile <script> etiketleri içindeki JSON verisi çekilir.
    • JSON Verisini İşleme: JSON verisi ayrıştırılarak ürün bilgileri ekrana yazdırılı
  • 04-01-2025, 01:02:39
    #2
    Her nedense bu bende çalışmıyor. HB bir süredir sistemini değiştirdi ve veri çekemiyorum. Sizde de durum aynı mı? @shms
  • 20-06-2025, 00:05:47
    #3
    Şuan yeni geliştirdiği güçlü bot koruması ile bu sistem maalesef çalışmayabilir. Ama asla aşılamaz değil...