Asyncio ve aiohttp ile Asenkron Proxy Kullanımı
1 viewsAsyncio ve aiohttp ile Asenkron Proxy Kullanımı
Web scraping projelerinde yüksek hacimli istek atarken senkron yöntemler (requests, Selenium) yavaş kalabilir. Bu durumda asyncio + aiohttp ile asenkron proxy kullanımı çok daha yüksek performans sağlar.
Bu makalede asyncio ve aiohttp ile proxy kullanımını adım adım anlatacağız.
1. Neden Asenkron Proxy Kullanmalıyız?
| Yöntem | Performans | Proxy Rotasyonu | Yüksek Hacim | Öneri |
|---|---|---|---|---|
| requests | Orta | Kolay | Zayıf | Küçük projeler |
| Selenium | Düşük | Zor | Çok Zayıf | Tarayıcı gerektiren işler |
| asyncio + aiohttp | Yüksek | Kolay | Çok İyi | Büyük projeler |
2. Gerekli Kütüphanelerin Yüklenmesi
Bash
pip install aiohttp aiohttp-socks
aiohttp-socks kütüphanesi SOCKS5 proxy desteği için gereklidir.
3. aiohttp ile Basit Proxy Kullanımı
Python
import asyncio import aiohttp async def fetch_with_proxy(url, proxy): async with aiohttp.ClientSession() as session: try: async with session.get(url, proxy=proxy, timeout=10) as response: return await response.text() except Exception as e: return f"Hata: {e}" # Kullanım proxy = "http://kullanici:sifre@proxy_ip:8080" result = asyncio.run(fetch_with_proxy("https://httpbin.org/ip", proxy)) print(result)
4. SOCKS5 Proxy ile aiohttp Kullanımı
Python
import asyncio import aiohttp from aiohttp_socks import ProxyConnector async def fetch_socks5(url, proxy): connector = ProxyConnector.from_url(proxy) # socks5://... async with aiohttp.ClientSession(connector=connector) as session: async with session.get(url) as response: return await response.text() proxy = "socks5://kullanici:sifre@proxy_ip:1080" result = asyncio.run(fetch_socks5("https://httpbin.org/ip", proxy)) print(result)
5. Asenkron Proxy Rotasyonu (En Önemli Kısım)
Birden fazla proxy ile yüksek performanslı rotasyon:
Python
import asyncio import aiohttp import random from aiohttp_socks import ProxyConnector PROXIES = [ "http://proxy1:8080", "http://kullanici:sifre@proxy2:3128", "socks5://proxy3:1080" ] async def fetch(url): proxy = random.choice(PROXIES) if proxy.startswith("socks5"): connector = ProxyConnector.from_url(proxy) session = aiohttp.ClientSession(connector=connector) else: session = aiohttp.ClientSession() try: async with session.get(url, proxy=proxy if not proxy.startswith("socks5") else None, timeout=10) as response: return await response.text() except Exception as e: return f"Hata ({proxy}): {e}" finally: await session.close() async def main(): urls = ["https://httpbin.org/ip"] * 20 tasks = [fetch(url) for url in urls] results = await asyncio.gather(*tasks) for result in results: print(result) asyncio.run(main())
6. En İyi Uygulamalar
| Uygulama | Açıklama |
|---|---|
| Proxy rotasyonu | Her istekte farklı proxy |
| Semaphore ile limit koyma | Aynı anda maksimum istek sayısını sınırlama |
| Hata yönetimi | try-except + retry mekanizması |
| Timeout kullanımı | Her istek için timeout belirleme |
| Residential/Mobile Proxy | Yüksek hacimli işler için önerilir |
7. Avantajları
- Çok yüksek performans (binlerce istek/dakika)
- Proxy rotasyonunu kolay yönetme
- Daha az kaynak tüketimi
- Büyük ölçekli scraping projeleri için ideal
Sonuç
asyncio + aiohttp kombinasyonu, yüksek hacimli web scraping projelerinde proxy kullanımını hem daha hızlı hem de daha yönetilebilir hale getirir. Özellikle proxy rotasyonu ile birleştirildiğinde çok güçlü bir yapı ortaya çıkar.
Profesyonel seviyede scraping yapmak istiyorsanız bu yöntemi öğrenmenizi şiddetle tavsiye ederiz.
Bir sonraki makalemizde Proxy ile Hata Yönetimi ve Retry Stratejileri konusunu detaylı olarak inceleyeceğiz.