Diskussion

Hier ist ein Python-Web-Scraper für Hacker News: ```python import requests from bs4 import BeautifulSoup import pandas as pd import time def scrape_hacker_news(pages=1): """ Scrapes Hacker News front page and returns a DataFrame with stories. Args: pages (int): Number of pages to scrape (default: 1) Returns: pd.DataFrame: DataFrame with story titles, links, points, and comments """ base_url = "https://news.ycombinator.com/" stories = [] for page in range(1, pages + 1): url = base_url if page == 1 else f"{base_url}?p={page}" try: response = requests.get(url, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' }) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # Find all story rows story_rows = soup.select('.athing') for row in story_rows: # Extract title and link title_element = row.select_one('.titleline > a') if not title_element: continue title = title_element.get_text(strip=True) link = title_element.get('href', '') # Extract metadata (points, comments, author) subtext = row.find_next_sibling('tr') points = 0 comments = 0 author = '' if subtext: points_element = subtext.select_one('.score') if points_element: points = int(points_element.get_text().replace(' points', '')) comments_element = subtext.select_one('.subtext > a:last-child') if comments_element and 'comment' in comments_element.get_text(): comments_text = comments_element.get_text() comments = int(comments_text.split()[0]) if comments_text.split()[0].isdigit() else 0 author_element = subtext.select_one('.hnuser') if author_element: author = author_element.get_text(strip=True) stories.append({ 'title': title, 'link': link, 'points': points, 'comments': comments, 'author': author, 'page': page }) print(f"Scraped page {page} - found {len(story_rows)} stories") time.sleep(1) # Be polite to the server except requests.RequestException as e: print(f"Error scraping page {page}: {e}") continue return pd.DataFrame(stories) def main(): # Scrape 2 pages of Hacker News df = scrape_hacker_news(pages=2) if not df.empty: # Sort by points (most popular first) df = df.sort_values('points', ascending=False) # Save to CSV df.to_csv('hacker_news_stories.csv', index=False) # Print top 10 stories print("\nTop 10 stories by points:") print(df.head(10)[['title', 'points', 'comments', 'author']].to_string(index=False)) print(f"\nTotal stories scraped: {len(df)}") print("Data saved to 'hacker_news_stories.csv'") else: print("No stories scraped. Check your internet connection or try again.") if __name__ == "__main__": main() ``` **Key Features:** - Scrapes story titles, links, points, comments, and authors - Handles pagination (scrape multiple pages) - Includes polite delays between requests - Saves results to CSV - Error handling for network issues - Uses BeautifulSoup for HTML parsing **Installation:** ```bash pip install requests beautifulsoup4 pandas ``` **Usage:** Run the script directly, or import the function: ```python from scraper import scrape_hacker_news df = scrape_hacker_news(pages=3) ``` **Note:** Always respect the website's `robots.txt` and terms of service. Hacker News allows scraping but be mindful of request frequency.

Von Wikiprompt, der freien Prompt-Enzyklopädie

Millie Marconi
Beigetragen vonMillie MarconiXQuelle

18. Juli 2025

Hier ist ein Python-Web-Scraper für Hacker News: ```python import requests from bs4 import BeautifulSoup import pandas as pd import time def scrape_hacker_news(pages=1): """ Scrapes Hacker News front page and returns a DataFrame with stories. Args: pages (int): Number of pages to scrape (default: 1) Returns: pd.DataFrame: DataFrame with story titles, links, points, and comments """ base_url = "https://news.ycombinator.com/" stories = [] for page in range(1, pages + 1): url = base_url if page == 1 else f"{base_url}?p={page}" try: response = requests.get(url, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' }) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # Find all story rows story_rows = soup.select('.athing') for row in story_rows: # Extract title and link title_element = row.select_one('.titleline > a') if not title_element: continue title = title_element.get_text(strip=True) link = title_element.get('href', '') # Extract metadata (points, comments, author) subtext = row.find_next_sibling('tr') points = 0 comments = 0 author = '' if subtext: points_element = subtext.select_one('.score') if points_element: points = int(points_element.get_text().replace(' points', '')) comments_element = subtext.select_one('.subtext > a:last-child') if comments_element and 'comment' in comments_element.get_text(): comments_text = comments_element.get_text() comments = int(comments_text.split()[0]) if comments_text.split()[0].isdigit() else 0 author_element = subtext.select_one('.hnuser') if author_element: author = author_element.get_text(strip=True) stories.append({ 'title': title, 'link': link, 'points': points, 'comments': comments, 'author': author, 'page': page }) print(f"Scraped page {page} - found {len(story_rows)} stories") time.sleep(1) # Be polite to the server except requests.RequestException as e: print(f"Error scraping page {page}: {e}") continue return pd.DataFrame(stories) def main(): # Scrape 2 pages of Hacker News df = scrape_hacker_news(pages=2) if not df.empty: # Sort by points (most popular first) df = df.sort_values('points', ascending=False) # Save to CSV df.to_csv('hacker_news_stories.csv', index=False) # Print top 10 stories print("\nTop 10 stories by points:") print(df.head(10)[['title', 'points', 'comments', 'author']].to_string(index=False)) print(f"\nTotal stories scraped: {len(df)}") print("Data saved to 'hacker_news_stories.csv'") else: print("No stories scraped. Check your internet connection or try again.") if __name__ == "__main__": main() ``` **Key Features:** - Scrapes story titles, links, points, comments, and authors - Handles pagination (scrape multiple pages) - Includes polite delays between requests - Saves results to CSV - Error handling for network issues - Uses BeautifulSoup for HTML parsing **Installation:** ```bash pip install requests beautifulsoup4 pandas ``` **Usage:** Run the script directly, or import the function: ```python from scraper import scrape_hacker_news df = scrape_hacker_news(pages=3) ``` **Note:** Always respect the website's `robots.txt` and terms of service. Hacker News allows scraping but be mindful of request frequency. Ein Prompt, der eine KI auffordert, ein Python-Skript mit BeautifulSoup zu generieren, um die Top 10 Schlagzeilen von Hacker News zu extrahieren.

Prompt-InhaltSpeichern

🌐
Schreibe Python-Code, um die Top-10-Schlagzeilen von Hacker News mit BeautifulSoup zu scrapen.

Melde dich an, um den vollständigen Prompt zu sehen

Weiter mit:

Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz

Verwendung

Dieser Prompt ist für die Verwendung mit coding gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.

Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.

Referenzen

Kategorien:coding| twitter| python| web-scraping

Diskussion