chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
Write some simple python code to scrape a website
- User
Write some simple python code to scrape a website
- Assistant
Sure! Here's a simple Python script that uses the
requestsandBeautifulSouplibraries to scrape the titles of blog posts from a sample website.🚀 Goal:
Scrape article titles from a web page.
✅ Prerequisites:
Make sure you install the required libraries first:
pip install requests beautifulsoup4
🧠 Python Code:
import requests from bs4 import BeautifulSoup # URL of the website you want to scrape url = "https://example.com" # Send a GET request to the website response = requests.get(url) # Check if request was successful if response.status_code == 200: # Parse the HTML content using BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') # Find all the article titles (Example: assuming <h2> tags with class 'post-title') titles = soup.find_all('h2', class_='post-title') # Print each title for title in titles: print(title.get_text(strip=True)) else: print("Failed to retrieve the page. Status code:", response.status_code)
✅ Notes:
- Replace
"https://example.com"with the actual URL. - Adjust the tag and class names in
soup.find_all()depending on how the website is structured (e.g., might beh1,h3, or another class). - Always respect a site's
robots.txtand terms of service before scraping.
Would you like a version that scrapes a real news/blog site like
quotes.toscrape.com(a test site for web scraping)? - Replace
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.