2013-01-05 30 views
11

Xin chào, tôi đã sử dụng BeautifulSoup để trích xuất một số dữ liệu từ một trang web như sau:BeautifulSoup html csv

from BeautifulSoup import BeautifulSoup 
from urllib2 import urlopen 

soup = BeautifulSoup(urlopen('http://www.fsa.gov.uk/about/media/facts/fines/2002')) 

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

print table 

Điều này cho phép đầu ra sau đây:

[<table width="70%" class="table-horizontal-line"> 
<tr> 
<th>Amount</th> 
<th>Company or person fined</th> 
<th>Date</th> 
<th>What was the fine for?</th> 
<th>Compensation</th> 
</tr> 
<tr> 
<td><a name="_Hlk74714257" id="_Hlk74714257">&#160;</a>£4,000,000</td> 
<td><a href="/pages/library/communication/pr/2002/124.shtml">Credit Suisse First Boston International </a></td> 
<td>19/12/02</td> 
<td>Attempting to mislead the Japanese regulatory and tax authorities</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£750,000</td> 
<td><a href="/pages/library/communication/pr/2002/123.shtml">Royal Bank of Scotland plc</a></td> 
<td>17/12/02</td> 
<td>Breaches of money laundering rules</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£1,000,000</td> 
<td><a href="/pages/library/communication/pr/2002/118.shtml">Abbey Life Assurance Company ltd</a></td> 
<td>04/12/02</td> 
<td>Mortgage endowment mis-selling and other failings</td> 
<td>Compensation estimated to be between £120 and £160 million</td> 
</tr> 
<tr> 
<td>£1,350,000</td> 
<td><a href="/pages/library/communication/pr/2002/087.shtml">Royal &#38; Sun Alliance Group</a></td> 
<td>27/08/02</td> 
<td>Pension review failings</td> 
<td>Redress exceeding £32 million</td> 
</tr> 
<tr> 
<td>£4,000</td> 
<td><a href="/pubs/final/ft-inv-ins_7aug02.pdf" target="_blank">F T Investment &#38; Insurance Consultants</a></td> 
<td>07/08/02</td> 
<td>Pensions review failings</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£75,000</td> 
<td><a href="/pubs/final/spe_18jun02.pdf" target="_blank">Seymour Pierce Ellis ltd</a></td> 
<td>18/06/02</td> 
<td>Breaches of FSA Principles ("skill, care and diligence" and "internal organization")</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£120,000</td> 
<td><a href="/pages/library/communication/pr/2002/051.shtml">Ward Consultancy plc</a></td> 
<td>14/05/02</td> 
<td>Pension review failings</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£140,000</td> 
<td><a href="/pages/library/communication/pr/2002/036.shtml">Shawlands Financial Services ltd</a> - formerly Frizzell Life &#38; Financial Planning ltd)</td> 
<td>11/04/02</td> 
<td>Record keeping and associated compliance breaches</td> 
<td>&#160;</td> 
</tr> 
<tr> 
<td>£5,000</td> 
<td><a href="/pubs/final/woodwards_4apr02.pdf" target="_blank">Woodward's Independent Financial Advisers</a></td> 
<td>04/04/02</td> 
<td>Pensions review failings</td> 
<td>&#160;</td> 
</tr> 
</table>] 

Tôi muốn xuất khẩu này vào CSV trong khi vẫn giữ cấu trúc bảng như được hiển thị trên trang web, điều này có khả thi không và nếu như thế thì sao?

Cảm ơn bạn đã trợ giúp.

+1

Bạn có thể muốn xem xét giải pháp này - http : //sebsauvage.net/python/html2csv.py. Tìm thấy nó bằng cách Googling "html to csv python" :) – Infinity

+0

Cảm ơn, mặc dù giải pháp đó có vẻ khá phức tạp? Tôi hy vọng có một cách dễ dàng hơn xem xét tôi có tất cả các dữ liệu trong một định dạng tương đối sạch sẽ ... nếu không, tôi sẽ cố gắng để làm theo này :-) –

Trả lời

23

Đây là một điều cơ bản bạn có thể thử. Điều này làm cho giả định rằng headers là tất cả trong các thẻ <th> và rằng tất cả dữ liệu tiếp theo nằm trong các thẻ <td>. Điều này làm việc trong trường hợp duy nhất bạn cung cấp, nhưng tôi chắc chắn điều chỉnh sẽ là cần thiết nếu các trường hợp khác :) Ý tưởng chung là khi bạn tìm thấy table (ở đây sử dụng find để kéo cái đầu tiên), chúng tôi nhận được headers bằng cách lặp lại thông qua tất cả các yếu tố th, lưu trữ chúng trong danh sách. Sau đó, chúng tôi tạo danh sách rows sẽ chứa danh sách đại diện cho nội dung của mỗi hàng. Điều này được điền bằng cách tìm tất cả các phần tử td dưới các thẻ tr và lấy text, mã hóa nó bằng UTF-8 (từ Unicode). Sau đó bạn mở một CSV, viết headers đầu tiên và sau đó viết tất cả các rows, but using (hàng cho hàng trong hàng nếu hàng) `để loại bỏ bất kỳ hàng trống):

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, 
+0

Cảm ơn, điều này trông giống như các giải pháp hoàn hảo. Tuy nhiên, tôi dường như nhận được một SyntaxError với dòng 'cat output_file.csv', nó chỉ đọc cú pháp không hợp lệ? –

+1

@ merlin_1980 Oh Tôi xin lỗi, nên đã đề cập đến đó là một điều cụ thể về IPython (về cơ bản chỉ cố gắng hiển thị nội dung của tệp). Nếu bạn nhận được đến thời điểm đó, bạn nên có tập tin được lưu trong thư mục đó. – RocketDonkey

+0

Cảm ơn rất nhiều :-) Tôi không nghĩ đến việc tìm kiếm trong thư mục và mở tệp bằng tay! –

Các vấn đề liên quan