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…

Python Turtle Example: Flappy Bird Development in under 10 mins!

Python Turtle Example | Develop Flappy Bird Clone in 10 mins In the following tutorial, we have used following Python libraries:
1. random: To generate obstacles at random position. 2. turtle: To help provide directions to the player. 3. vector from freegames: To 2-dimensional vectors.
Before developing the game, here is a screenshot of what you'll be developing in 10 mins.
Steps to create Python Flappy Bird Game:
1. Open Pycharm and install 'freegames' package by going to Files > Settings > Project Interpreter. 2. Click on '+' sign on right and search for 'freegames' package. 3. Import packages in your project by writing following lines of code:
from random import * from turtle import * from freegames import vector 4. There are two variables required in the game, one for Bird object and another for an array of obstacles:
bird = vector(0, 0) balls = []
5. Create function for the player response on screen tap:
def tap(x, y): "Move bird up in res…

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…