write a crawler for ricardo.ch
- User
write a crawler for ricardo.ch
- Assistant
To create a web crawler for the website ricardo.ch, we can use Python and two main libraries: Requests and BeautifulSoup. Requests will help fetch a webpage's content, and BeautifulSoup will parse and navigate the HTML structure.
Please note that before creating a crawler for any website, review the website's robots.txt file and the legal terms and conditions to ensure you don't breach any rules.
Here's a simple example of a web crawler that scrapes the title and price of products from the first page of keyword search results:
import requests from bs4 import BeautifulSoup # Function to fetch data from ricardo.ch def ricardo_crawler(keyword, page=1): url = f'https://www.ricardo.ch/de/s/{keyword}?page={page}' reqs = requests.get(url) soup = BeautifulSoup(reqs.text, 'html.parser') products = [] for item in soup.find_all('div', {'class': 'rdo-sc-ad-list-item-info'}): try: title = item.find('h3', {'class': 'rdo-sc-ad-list-item-title-container'}).text.strip() price = item.find('span', {'class': 'rdo-sc-price'}).text.strip() products.append((title, price)) except AttributeError: continue return products # Test the function if __name__ == '__main__': keyword = 'laptop' products = ricardo_crawler(keyword) for i, product in enumerate(products): print(f"{i + 1}. {product[0]} - {product[1]}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.