Prompt Details
Model
(gemini-3.1-pro)
Token size
204
Example input
[Target Task]: Scrape product prices and titles from an e-commerce page
[Preferred Library]: BeautifulSoup and Requests
[Additional Requirements]: Save output to a CSV file named products.csv
Example output
Here is the Python script using BeautifulSoup and Requests to scrape the products. Make sure to install the required libraries by running `pip install requests beautifulsoup4` before executing the script.
import requests
from bs4 import BeautifulSoup
import csv
import time
import random
def scrape_ecommerce_products(url, output_file='products.csv'):
# Anti-bot: Use a common User-Agent
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
try:
# Step 1: Send HTTP GET request
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # Check for HTTP errors
# Step 2: Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Step 3: Find all product containers (Note: Class names are examples and should be updated based on the actual target website)
products = soup.find_all('div', class_='product-item')
scraped_data = []
for product in products:
# Step 4: Extract title and price with error handling
title_element = product.find('h2', class_='product-title')
price_element = product.find('span', class_='product-price')
title = title_element.text.strip() if title_element else 'N/A'
price = price_element.text.strip() if price_element else 'N/A'
scraped_data.append({'Title': title, 'Price': price})
# Step 5: Save data to CSV
if scraped_data:
with open(output_file, mode='w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=['Title', 'Price'])
writer.writeheader()
writer.writerows(scraped_data)
print(f"Successfully saved {len(scraped_data)} products to {output_file}")
else:
print("No products found to save.")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the HTTP request: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Example usage
if __name__ == "__main__":
target_url = 'https://example.com/products'
# Anti-bot: Random delay before execution
time.sleep(random.uniform(1.0, 3.0))
scrape_ecommerce_products(target_url)
By purchasing this prompt, you agree to our terms of service
GEMINI-3.1-PRO
Instantly generate robust Python scripts for browser automation and web scraping! Perfect for developers, data analysts, and marketers who want to automate repetitive web tasks. This prompt creates clean, well-commented code using libraries like Selenium or Playwright, complete with error handling and anti-blocking mechanisms. Save hours of coding and debugging.
...more
Added over 1 month ago
