0

I'm trying to scrape the table in the following website but was not able to do it:

https://www.moneycontrol.com/financials/relianceindustries/ratiosVI/RI?classic=true#RI

import csv

from bs4 import BeautifulSoup

from urllib.request import urlopen

soup = BeautifulSoup(urlopen('https://www.moneycontrol.com/financials/relianceindustries/ratiosVI/RI?classic=true#RI'))

table = soup.find('table', attrs={ "class" : "table-horizontal-line"})

headers = [header.text for header in table.find_all('th')]

rows = []

for row in table.find_all('tr'):
    rows.append([val.text.encode('utf8') for val in row.find_all('td')])

with open('output_file.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerow(headers)
    writer.writerows(row for row in rows if row)
4
  • 2
    What is the problem? Any errors? Commented Jan 2, 2019 at 18:57
  • Which part of your code fails? Can you access the site? Can you find the table? Can you extract the rows? Can you write the csv? Commented Jan 2, 2019 at 19:03
  • Try doing something = table.find_all('tr')' on a separate line, then go into the loop for row in something:` but that's my best guess without more information on whats wrong Commented Jan 2, 2019 at 19:06
  • I take it you mean "scrape" and "scraper". Anyway, are you sure "table-horizontal-line" is not only in the code, but a class of the table itself and not for example a row or other tag? I don't see it when I look at the page's code. There might be a better way to identify the table. Commented Jan 2, 2019 at 19:16

1 Answer 1

1

You can use pandas for this. There are a couple of rows at the top you may wish to remove and replace some other NaNs with empty strings as cleansing.

import pandas as pd
result = pd.read_html('https://www.moneycontrol.com/financials/relianceindustries/ratiosVI/RI?classic=true#RI')
df = result[3].dropna(how='all').fillna('')
df.to_csv(r'C:\Users\User\Desktop\Data.csv', sep=',', encoding='utf-8',index = False )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.