{"id":1816,"date":"2026-02-17T14:35:21","date_gmt":"2026-02-17T09:05:21","guid":{"rendered":"https:\/\/sharanamshah.com\/mymusings\/?p=1816"},"modified":"2026-02-17T14:36:18","modified_gmt":"2026-02-17T09:06:18","slug":"python-automation-how-to-build-a-daily-news-scraper","status":"publish","type":"post","link":"https:\/\/sharanamshah.com\/mymusings\/blog\/2026\/02\/17\/python-automation-how-to-build-a-daily-news-scraper\/","title":{"rendered":"Python Automation: How to Build a Daily News Scraper"},"content":{"rendered":"<h1>Automate Your Morning: Build a News Scraper with Python<\/h1>\n<p>Hello there, friend! I am so glad you are here. Today, we are going to dive into the world of <b>Python Automation<\/b>. If you have ever felt like you are doing the same thing over and over again on your computer, you are in the right place. We are going to build something that does the work for you!<\/p>\n<h2>The Real-World Scenario<\/h2>\n<p>Imagine you are a busy <b>Market Researcher<\/b>. Every single morning, you have to visit five different news websites to see what the top trending stories are. You copy the headlines and paste them into a spreadsheet. It takes 20 minutes of your time every day. That\u2019s over 120 hours a year spent just copying and pasting!<\/p>\n<p><b>The Analogy:<\/b> Think of this script like a &#8220;Digital Personal Assistant.&#8221; Instead of you walking to the newsstand, picking up the paper, and highlighting the headlines with a pen, your assistant flies there in a split second, snaps a photo of the front page, and neatly types out the titles on your desk before you even finish your first sip of coffee.<\/p>\n<h2>The Solution<\/h2>\n<p>We are going to write a Python script that visits a popular news site (Hacker News), looks for the titles of the top articles, and saves them into a CSV file (which you can open in Excel). This is a classic example of &#8220;Web Scraping,&#8221; a powerful way to automate data collection.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Don&#8217;t worry, setting this up is super easy! You just need to install two &#8220;libraries&#8221; (tools that help Python talk to websites). Open your terminal or command prompt and type:<\/p>\n<pre>pip install requests beautifulsoup4<\/pre>\n<h2>The Code<\/h2>\n<p>Here is your copy-paste ready script. I have made sure every line is explained so you know exactly what is happening. Let\u2019s look how cool this is!<\/p>\n<pre>\n\"\"\"\n-----------------------------------------------------------------------\nAuthors: Sharanam & Vaishali Shah\n-----------------------------------------------------------------------\nRecipe: Automated Web Scraper for Daily Headlines\n-----------------------------------------------------------------------\n\"\"\"\n\nimport requests  # This is our 'Mailman'\u2014it fetches the webpage for us.\nfrom bs4 import BeautifulSoup  # This is our 'Organizer'\u2014it makes sense of the HTML.\nimport csv  # This is our 'Secretary'\u2014it writes the data into a file.\n\ndef scrape_top_news():\n    # 1. The address of the website we want to visit\n    url = 'https:\/\/news.ycombinator.com\/'\n    \n    print(\"Starting the automation... please wait a second!\")\n    \n    try:\n        # 2. Ask the Mailman to get the page content\n        response = requests.get(url)\n        response.raise_for_status() # This checks if the website is actually working\n        \n        # 3. Hand the messy webpage code to the Organizer\n        soup = BeautifulSoup(response.text, 'html.parser')\n        \n        # 4. Find all the story titles (on this site, they are in 'span' tags with class 'titleline')\n        # We use a list to store our findings\n        headlines_list = []\n        stories = soup.find_all('span', class='titleline')\n        \n        for story in stories:\n            title = story.text\n            link = story.find('a')['href']\n            headlines_list.append([title, link])\n        \n        # 5. Tell the Secretary to save this into a CSV file\n        with open('daily_news_report.csv', 'w', newline='', encoding='utf-8') as file:\n            writer = csv.writer(file)\n            # Add a header row\n            writer.writerow(['Headline', 'Link'])\n            # Add all the stories we found\n            writer.writerows(headlines_list)\n            \n        print(\"Success! Your 'daily_news_report.csv' is ready for you to view.\")\n        \n    except Exception as e:\n        print(f\"Oops! Something went wrong: {e}\")\n\n# This tells Python to run the function we just made\nif __name__ == \"__main__\":\n    scrape_top_news()\n<\/pre>\n<h2>Code Walkthrough<\/h2>\n<p>Let&#8217;s break it down together, line by line:<\/p>\n<ul>\n<li><b>import requests:<\/b> We are bringing in a tool that allows Python to go out to the internet and &#8220;request&#8221; a website&#8217;s data.<\/li>\n<li><b>BeautifulSoup:<\/b> Websites are written in a language called HTML, which looks like a mess to humans. This tool &#8220;cleans it up&#8221; so we can search for specific things, like titles.<\/li>\n<li><b>requests.get(url):<\/b> This is like typing the URL into your browser and hitting Enter.<\/li>\n<li><b>soup.find_all:<\/b> We are telling Python, &#8220;Go through the whole page and find every single title.&#8221; It\u2019s like using &#8216;Find&#8217; (Ctrl+F) on a document.<\/li>\n<li><b>csv.writer:<\/b> This creates a file that Excel can read. We give it a header so you know which column is the headline and which is the link.<\/li>\n<\/ul>\n<h2>Sample Output<\/h2>\n<p>When you run this script, you will see this in your console:<\/p>\n<pre>\nStarting the automation... please wait a second!\nSuccess! Your 'daily_news_report.csv' is ready for you to view.\n<\/pre>\n<p>And in your folder, you will find a new file named <b>daily_news_report.csv<\/b>. If you open it, you\u2019ll see two neat columns with the latest tech news!<\/p>\n<h2>Conclusion<\/h2>\n<p>Look at that! You just built a working automation tool. You\u2019ve moved from manually browsing to having a script do the &#8220;boring stuff&#8221; for you. Don&#8217;t worry if it felt like a lot at first\u2014the more you play with it, the easier it gets. Try changing the URL to another site or changing the filename. Stay curious and keep coding!<\/p>\n<hr>\n<div class=\"book-promo\" style=\"background-color: #f8f9fa; padding: 25px; border-left: 6px solid #d63638; margin-top: 40px; border-radius: 4px;\">\n<h3 style=\"margin-top:0; color: #23282d;\">\ud83d\ude80 Ready to Master Python Automation?<\/h3>\n<p style=\"font-size: 16px;\">This tutorial was just a small taste of what is possible. If you want to dive deeper and build professional-grade applications, my book is the perfect guide.<\/p>\n<p><strong>Detailed in: <a href=\"https:\/\/sharanamshah.com\/mymusings\/home\/books-ive-authored\/automate-everyday-tasks-with-python-for-beginners\/\" target=\"_blank\">Python Automation Engineering for Professionals<\/a><\/strong><\/p>\n<p>In the book, I cover this concept along with advanced patterns, best practices, and real-world projects. Don&#8217;t just copy code\u2014understand the architecture.<\/p>\n<p style=\"margin-bottom: 0;\">\n            <a href=\"https:\/\/sharanamshah.com\/mymusings\/home\/books-ive-authored\/automate-everyday-tasks-with-python-for-beginners\/\" style=\"background-color: #d63638; color: white; padding: 12px 20px; text-decoration: none; border-radius: 4px; font-weight: bold; display: inline-block;\">Get the Book Now &rarr;<\/a>\n        <\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to automate your daily information gathering using Python. Sharanam Shah teaches you how to scrape headlines and save them to a file automatically.<\/p>\n","protected":false},"author":1,"featured_media":1989,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[94],"tags":[],"class_list":["post-1816","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-everyday-automation-hacks"],"_links":{"self":[{"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/posts\/1816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/comments?post=1816"}],"version-history":[{"count":0,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/posts\/1816\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/media\/1989"}],"wp:attachment":[{"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/media?parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/categories?post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sharanamshah.com\/mymusings\/wp-json\/wp\/v2\/tags?post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}