Wednesday, January 11, 2017

Python BeautifulSoup Example

#!/usr/bin/python

from urllib import urlopen
from bs4 import BeautifulSoup

url = 'https://www.theice.com/marketdata/reports/icefutureseurope/BrentMarkers.shtml'

html = urlopen(url)

bsObj = BeautifulSoup(html.read())

# The markers are in a table, there are several tables on the page
# The Afternoon markers are in the table that follows a paragraph with
# the text ICE BRENT AFTERNOON MARKERS

# Find all the tables in the document
tables = bsObj.findAll('table', {'class':'table table-responsive table-data table-align-left'})

table_body = None

# Iterate over the tables looking at the previous paragraph
for table in tables:

   p = table.find_previous('p')

   # Check for the afternoon markers text
   if p.get_text() == 'ICE BRENT AFTERNOON MARKERS':

      print(p.get_text())

      # Extract that table  
      table_body = table.find('tbody')

      break

if table_body <> None:

   # Get all the rows
   rows = table_body.find_all('tr')

   # Print each row
   for row in rows:

      cols = row.find_all('td')

      print('%s %s %s' % (cols[0].text.strip(), cols[1].text.strip(), cols[2].text.strip()))

else:

   print('No markers!')

raw_input('Press Enter to continue ...')

##END##


No comments: