Python script to scrape a table from a website and print table to console.

I've started a python script to scrape a table from a website and print to console, but I'm having trouble displaying the content in the table format as displayed on the website. I'd appreciate some help with the information formatting and getting information from individual cells to be used for calculations.

import os
import os.path
import requests
from bs4 import BeautifulSoup
url = 'https://www.hl.co.uk/shares/shares-search-results/a/aviva-plc-ordinary-25p/financial-statements-and-reports'
headers= {'User-Agent': 'Mozzilla/5.0'}
response = requests.get(url, headers = headers)
soup = BeautifulSoup(response.content, 'html.parser')
stat_table =soup.find_all('table', class_ ='factsheet-table responsive')
stat_table = stat_table[0]
len(stat_table)

for row in stat_table.find_all('tr'):
	for cell in row.find_all('td'):
		print(cell.text)

Open in new window

LVL 6
patelbg2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Use tab as your end-of-cell string:
print(cell.text, end='\t')

Open in new window

0
patelbg2001Author Commented:
# -----------------------------------------------------------------------------
# Created:     04.03.2014
# Copyright:   (c) Josua Schmid 2014
# Licence:     AGPLv3
#
# Sample script for parsing HTML tables
# -----------------------------------------------------------------------------

using a code example from above.

import urllib.request
import pandas as pd
import sys
import os

sys.path.append('/Users/bhav\ 1/Documents/keepbak2/python')

from html_table_parser import HTMLTableParser

target = 'url'

#get website config
req = urllib.request.Request(url=target)
f = urllib.request.urlopen(req)
xhtml = f.read().decode('utf-8')

#instantiate he parser and feed it
p = HTMLTableParser()
p.feed(xhtml)

#write the table into python list

cwd = os.getcwd()  # Get the current working directory (cwd)
print(cwd)

Table_List = p.tables

# Use the Pandas from_dict method to read the tables into
# a Pandas data frame

df = pd.DataFrame.from_dict(Table_List[0])
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.