This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here,
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
- 30 Days Of Python 👨💻 - Day 12 - Lambda Expression And Comprehensions
- 30 Days Of Python 👨💻 - Day 13 - Decorators
- 30 Days Of Python 👨💻 - Day 14 - Handling Errors
- 30 Days Of Python 👨💻 - Day 15 - Generators
- 30 Days Of Python 👨💻 - Day 16 - Module Basics
- 30 Days Of Python 👨💻 - Day 17 - External Modules
- 30 Days of Python 👨💻 - Day 18 - File I/O
- 30 Days Of Python 👨💻 - Day 19 - Regular Expressions
- 30 Days Of Python 👨💻 - Day 20 - Debugging And Testing
- 30 Days Of Python 👨💻 - Day 21 - Scripting Basics
- 30 Days of Python 👨💻 - Day 22 - Scripting Extras
Web Scraping is the technique for extracting data from a website by crawling it. It is mainly used to collect meaningful data from websites especially when there are no available APIs to extract information. Today I explored the basics of web scraping with Python and would like to share my experience.
Scraping is a form of scripting that allows us to automate the process of extracting large unstructured data from websites and organize it in a structured way to use it for several purposes such as gathering emails, product price, stock prices, flight data or any other relevant information. Doing such things manually takes a lot of time and effort. Python has some amazing libraries to make web scraping quite an easier and fun task to implement. I mainly explored the most basic and popular library Beautiful Soup to familiarize myself with the concept.
Good Practices
Web Scraping is extremely powerful and there is a lot of debate over its uses. Most websites have a robots.txt file which mentions which specific URLs should be crawled (scraped) and which should not be. This file is mainly an instruction for various search engine bots like Google bot, Yahoo bot, Bing bot etc as to which specific pages they should crawl for search engine optimization. So all search engine crawlers are mainly web scrapers that extract data from the website to rank them as per relevant keywords.
However, a website cannot literally restrict a web scraping program to not crawl its data even if it is disallowed in the robots.txt file. It is a good and ethical practice to go over a website’s robots.txt file if present and extract data from only mentioned URLs to prevent any kind of data breach issues.
Scraping using Beautiful Soup
For today’s session, I decided to try extracting data from the Hacker News website - An extremely popular website among the dev community. These are the rules defined in its robots.txt file
- User-Agent: *
- Disallow: /x?
- Disallow: /vote?
- Disallow: /reply?
- Disallow: /submitted?
- Disallow: /submitlink?
- Disallow: /threads?
- Crawl-delay: 30
So we are allowed to crawl and fetch data from the news page https://news.ycombinator.com/newest which lists the latest articles from the development world. The goal is to crawl the first 5 pages and extract the articles with at-least 100 points along with their links. This can be pretty useful to automatically fetch all highly voted items and read them from the terminal itself without having to visit hacker news website and manually search for popular posts.
First two libraries need to be installed, requests for doing HTTP requests and beautifulsoup4 for scraping the website.
pip install requests pip install beautifulsoup4
hacker_news_scraper.py
- import requests
- from bs4 import BeautifulSoup
- BASE_URL = 'https://news.ycombinator.com'
- response = requests.get(BASE_URL)
- # extract the text content of the web page
- response_text = response.text
- # parse HTML
- soup = BeautifulSoup(response_text, 'html.parser')
- print(soup.prettify()) # prints the html content in a readable format
The documentation for Beautiful Soup https://www.crummy.com/software/BeautifulSoup/bs4/doc/ showcases the various use cases. Using the browser’s inspect element tools, the selector for the elements can be viewed and then used for extracting data. In this case, all the articles have a storylink class and their associated points have the class score. These selectors can now be used to fetch the respective data and combine them.
- # extract all the links using the class selector
- links_list = soup.select('.storylink')
- # extract all the points using the class selector
- points_list = soup.select('.score')
After looping through the links, the associated, title, link and their points can be combined as a dictionary object and then appended to a popular posts list.
It is to be noted that the enumerate function is used to get the index of each element to fetch the respective points as the points are not contained within the links container.
Only posts with a minimum of 100 points are appended to popular lists.

Join the conversation! Your thoughts help the community grow.