Skip to main content

Using BeautifulSoup Python Example Web Scraping Stock prices from Bloomberg Website

In this tutorial on Python, we will use BeautifulSoup Python Example for web scraping current S&P 500 index prices available at Bloomberg website.

What is Web Scraping?

As a developer, most of us work on various projects that require data for which we are dependent on some websites like in this tutorial where are would need latest Stock prices in the market and for this, we are going to be dependent on Bloomberg website.
Extracting information from some website is known as Web Scraping and doing web scraping is somewhat illegal. Why so? well by doing web scraping though you are leveraging other developers work and at the same time, you are also utilizing their server and that too without their consent. Developing an app for stock market prices that uses a web scraping and publishing that on App store can actually get you a lifetime ban.

But don’t worry as this is just a tutorial and it won’t stress their servers. But if you are planning to develop some app that would need to stress on someone else server, you can use their APIs which some websites like Facebook, Google, and Uber provide or email the developer and ask for consent.

BeautifulSoup Python Example

BeautifulSoup Python library is used to extract information from any website or extract data from any HTML or XML files. In this tutorial, we will use BeautifulSoup Python Example for web scraping current Stock prices available at Bloomberg website. We will also use urllib2 library to get HTML page of the URL declared.

Software Used:

  1. PyCharm Community Edition, Download here.

Python Library Used:

  1. BeautifulSoup
  2. urllib2

Steps used:

1.Open Pycharm after installing and import urllib2 and BeautifulSoup Python library by going to File > Settings > Project Interpreter > Install, and search for these two libraries.

2. Write the code for importing urllib2 and BeautifulSoup Python library.

from bs4 import BeautifulSoup
import urllib2
 
3. Declare a variable that contains the link to Bloomberg website.

fimc ='http://www.bloomberg.com/quote/SPX:IND'
 
4. Then call the urllib2 urlopen function, get the HTML page of the website declared and return the HTML to a variable page.

page = urllib2.urlopen(fimc)
 
5. Next, I will parse the page into BeautifulSoup Python format and store it in a variable called ‘soup’.

soup = BeautifulSoup(page,'html.parser')
 
6. Next, I will take out the <div> of the name and get the current index name.

name_box = soup.find('h1', attrs={'class' : 'name'})
 
7. We will use the strip function now to remove starting and trailing. And finally, print the S&P 500 index name.

name = name_box.text.strip()
print name
 
8. Next, we will get the S&P 500 index price.

price_box = soup.find('div', attrs={'class':'price'})
price = price_box.text
print price
 
9. TaDa! Run this file and you’ll get the price.

You can find the complete code here:

BeautifulSoup Python Code
from bs4 import BeautifulSoup
import urllib2
fimc ='http://www.bloomberg.com/quote/SPX:IND'

page = urllib2.urlopen(fimc)

soup = BeautifulSoup(page,'html.parser')

name_box = soup.find('h1', attrs={'class' : 'name'})
name = name_box.text.strip()
print name


price_box = soup.find('div', attrs={'class':'price'})
price = price_box.text
print price

Comments

Popular posts from this blog

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.
How To Open PDF File in Android Application Example  In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.

Get inspirtion from here: Top 7 PDF Download for Mobile Android Apps

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.



 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I'v…

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.


Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.



4. Open AndroidManifest.xml…

Android Live Wallpaper Tutorial (using Android Studio 3.x) | FaultInMyCode

The Android Live Wallpapers are the way to redesign your mobile background into something amazing animated GIF using different effects and motions. Many novice developers are making good money by Henceforth here is Android Live Wallpaper tutorial for you to build your own Live wallpaper and start earning today as a developer!



This tutorial is a part of our Blog series on ‘Roadway to Android Development Course’, its suitable for Beginners to programming as well as the android enthusiast to learn about Android development and build their own apps.

In this tutorial for beginners, you’ll learn how by using Android Studio 3.0 and by having basic knowledge of Java, we will teach you to build your own Live wallpaper for Android which you may later publish in the Google Play Store. We assume you have some experience with Android studio, if not please check how to build your own calculator app and how to create simple login app in Android Studio 3.x tutorial.
How to build Android …