• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Python Tutorial
  • Python Basics
  • Python Code Examples

 

You are here: Home / Python On The Web / Web Scraping with BeautifulSoup

Web Scraping with BeautifulSoup

Last Updated: August 25, 2020

Web Scraping

“Web scraping (web harvesting or web data extraction) is a computer software technique of extracting information from websites.”

HTML parsing is easy in Python, especially with help of the BeautifulSoup library. In this post we will scrape a website (our own) to extract all URL’s.

Getting Started

To begin with, make sure that you have the necessary modules installed. In the example below, we are using Beautiful Soup 4 and Requests on a system with Python 2.7 installed. Installing BeautifulSoup and Requests can be done with pip:


$ pip install requests

$ pip install beautifulsoup4

What is Beautiful Soup?

On the top of their website, you can read: “You didn’t write that awful page. You’re just trying to get some data out of it. Beautiful Soup is here to help. Since 2004, it’s been saving programmers hours or days of work on quick-turnaround screen scraping projects.”

Beautiful Soup Features:

Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. It doesn’t take much code to write an application.

Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don’t have to think about encodings, unless the document doesn’t specify an encoding and Beautiful Soup can’t autodetect one. Then you just have to specify the original encoding.

Beautiful Soup sits on top of popular Python parsers like lxml and html5lib, allowing you to try out different parsing strategies or trade speed for flexibility.

Extracting URL’s from any website

Now when we know what BS4 is and we have installed it on our machine, let’s see what we can do with it.


from bs4 import BeautifulSoup

import requests

url = raw_input("Enter a website to extract the URL's from: ")

r  = requests.get("http://" +url)

data = r.text

soup = BeautifulSoup(data)

for link in soup.find_all('a'):
    print(link.get('href'))

When we run this program, it will ask us for a website to extract the URL’s from


Enter a website to extract the URL's from: www.pythonforbeginners.com
Learn Python By Example
https://www.pythonforbeginners.com/python-overview-start-here/ https://www.pythonforbeginners.com/dictionary/ https://www.pythonforbeginners.com/python-functions-cheat-sheet/
Lists
https://www.pythonforbeginners.com/loops/ https://www.pythonforbeginners.com/python-modules/ https://www.pythonforbeginners.com/strings/ https://www.pythonforbeginners.com/sitemap/ https://www.pythonforbeginners.com/feed/
Learn Python By Example
.... .... ....

I recommend that you read our introduction article: Beautiful Soup 4 Python to get more knowledge and understanding about Beautiful Soup.

More Reading

http://www.crummy.com/software/BeautifulSoup/

http://docs.python-requests.org/en/latest/index.html

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Beautiful Soup, Python On The Web, Requests Date Originally Published: March 9, 2016

More Python Topics

API Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line crawler Data Types Development Dictionary Dictionary Data Structure In Python envoy Errorhandling Error Handling Exceptions Fabric Files fnmatch ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python Code Snippets Python On The Web Python Strings Requests Scraping Scripts sh simplehttpserver System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Datacamp Review 2020
  • Most Common Python Interview Questions For 2020
  • Python 2 Vs Python 3 with Examples
  • How To Run Your Python Scripts
  • The 5 Best Python IDE’s and Code Editors for 2019

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 ยท PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us