jak scraopwać z ebay
- User
jak scraopwać z ebay
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
jak scraopwać z ebay
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Aby scrapować dane z eBay, potrzebujesz odpowiedniego podejścia, ponieważ eBay ma zabezpieczenia przeciwko automatycznemu scrapowaniu. Oto podstawowy przykład kodu w Pythonie z użyciem biblioteki requests i BeautifulSoup:
import requests
from bs4 import BeautifulSoup
import time
import random
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}
def scrape_ebay_search(search_term, max_pages=1):
results = []
for page in range(1, max_pages + 1):
url = f"https://www.ebay.com/sch/i.html?_nkw={search_term.replace(' ', '+')}&_pgn={page}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to retrieve page {page}: {response.status_code}")
continue
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.select('li.s-item')
for item in items:
title_element = item.select_one('h3.s-item__title')
price_element = item.select_one('span.s-item__price')
link_element = item.select_one('a.s-item__link')
if title_element and price_element and link_element:
title = title_element.text.strip()
price = price_element.text.strip()
link = link_element.get('href')
results.append({
'title': title,
'price': price,
'link': link
})
# Dodaj losowe opóźnienie między zapytaniami
time.sleep(random.uniform(2, 5))
return results
# Przykład użycia
products = scrape_ebay_search("laptop", max_pages=2)
for product in products[:5]: # Pokaż pierwsze 5 wyników
print(f"Title: {product['title']}")
print(f"Price: {product['price']}")
print(f"Link: {product['link']}")
print("-" * 50)
Czy chciałbyś, żebym wyjaśnił lub rozwinął ten kod?
Pamiętaj, że scrapowanie eBay może naruszać ich warunki korzystania z usługi. Lepszym rozwiązaniem jest korzystanie z oficjalnego API eBay, jeśli potrzebujesz danych do celów komercyjnych lub na większą skalę.