Вот основная вещь, которую вы можете попробовать. Это делает предположение, что headers
все в тегах <th>
, и что все последующие данные находятся в тегах <td>
. Это работает в единственном случае, который вы предоставили, но я уверен, что корректировки понадобятся в других случаях :) Общая идея заключается в том, что как только вы найдете свой table
(здесь, используя find
, чтобы вытащить первый), мы получаем headers
, итерируя все элементы th
, сохраняя их в списке. Затем мы создаем список rows
, который будет содержать списки, представляющие содержимое каждой строки. Это заполняется путем нахождения всех td
элементов под тегами tr
и с помощью text
, кодирующего его в UTF-8 (из Unicode). Затем вы открываете CSV, сначала записывая headers
, а затем записывая все rows, but using
(строка для строки в строках, если строка) `для устранения любых пустых строк):
In [117]: import csv
In [118]: from bs4 import BeautifulSoup
In [119]: from urllib2 import urlopen
In [120]: soup = BeautifulSoup(urlopen('http://www.fsa.gov.uk/about/media/facts/fines/2002'))
In [121]: table = soup.find('table', attrs={ "class" : "table-horizontal-line"})
In [122]: headers = [header.text for header in table.find_all('th')]
In [123]: rows = []
In [124]: for row in table.find_all('tr'):
.....: rows.append([val.text.encode('utf8') for val in row.find_all('td')])
.....:
In [125]: with open('output_file.csv', 'wb') as f:
.....: writer = csv.writer(f)
.....: writer.writerow(headers)
.....: writer.writerows(row for row in rows if row)
.....:
In [126]: cat output_file.csv
Amount,Company or person fined,Date,What was the fine for?,Compensation
" £4,000,000",Credit Suisse First Boston International ,19/12/02,Attempting to mislead the Japanese regulatory and tax authorities,
"£750,000",Royal Bank of Scotland plc,17/12/02,Breaches of money laundering rules,
"£1,000,000",Abbey Life Assurance Company ltd,04/12/02,Mortgage endowment mis-selling and other failings,Compensation estimated to be between £120 and £160 million
"£1,350,000",Royal & Sun Alliance Group,27/08/02,Pension review failings,Redress exceeding £32 million
"£4,000",F T Investment & Insurance Consultants,07/08/02,Pensions review failings,
"£75,000",Seymour Pierce Ellis ltd,18/06/02,"Breaches of FSA Principles (""skill, care and diligence"" and ""internal organization"")",
"£120,000",Ward Consultancy plc,14/05/02,Pension review failings,
"£140,000",Shawlands Financial Services ltd - formerly Frizzell Life & Financial Planning ltd),11/04/02,Record keeping and associated compliance breaches,
"£5,000",Woodward's Independent Financial Advisers,04/04/02,Pensions review failings,