HTML表格到CSV刮板

时间:2019-01-02 18:55:01

标签: python web-scraping jupyter-notebook export-to-csv

我正在尝试在以下网站中刮擦桌子,但无法做到:

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)

1 个答案:

答案 0 :(得分:1)

您可以为此使用熊猫。您可能希望在顶部删除几行,并用空字符串替换其他一些NaN来进行清理。

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 )