2017-09-15 17 views
1

私はPythonにはまったく新しいものですが、私はHTMLテーブルのデータをオンラインでリッピングし、それをCSVに印刷するWebスクラップツールを構築しようとしています。同じフォーマット。Python - WebのHTMLテーブルの掻き出しとCSVへの印刷

ここにHTMLテーブルのサンプルがあります(膨大なので、私はいくつかの行しか提​​供しません)。 「日付」、「オープン」、「高」、「低」、「閉じる」、「ボリューム」、「時価総額:

<div class="col-xs-12 tab-content"> 
     <div id="historical-data" class="tab-pane active"> 
      <div class="tab-header"> 
      <h2 class="pull-left bottom-margin-2x">Historical data for Bitcoin</h2> 

      <div class="clear"></div> 

      <div class="row"> 
       <div class="col-md-12"> 
       <div class="pull-left"> 
        <small>Currency in USD</small> 
       </div> 
       <div id="reportrange" class="pull-right"> 
        <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp; 
        <span>Aug 16, 2017 - Sep 15, 2017</span> <b class="caret"></b> 
       </div> 
       </div> 
      </div> 

      <table class="table"> 
       <thead> 
       <tr> 
       <th class="text-left">Date</th> 
       <th class="text-right">Open</th> 
       <th class="text-right">High</th> 
       <th class="text-right">Low</th> 
       <th class="text-right">Close</th> 
       <th class="text-right">Volume</th> 
       <th class="text-right">Market Cap</th> 
       </tr> 
       </thead> 
       <tbody> 

       <tr class="text-right"> 
        <td class="text-left">Sep 14, 2017</td> 
        <td>3875.37</td>  
        <td>3920.60</td> 
        <td>3153.86</td> 
        <td>3154.95</td> 
        <td>2,716,310,000</td> 
        <td>64,191,600,000</td> 
       </tr> 

       <tr class="text-right"> 
        <td class="text-left">Sep 13, 2017</td> 
        <td>4131.98</td>  
        <td>4131.98</td> 
        <td>3789.92</td> 
        <td>3882.59</td> 
        <td>2,219,410,000</td> 
        <td>68,432,200,000</td> 
       </tr> 

       <tr class="text-right"> 
        <td class="text-left">Sep 12, 2017</td> 
        <td>4168.88</td>  
        <td>4344.65</td> 
        <td>4085.22</td> 
        <td>4130.81</td> 
        <td>1,864,530,000</td> 
        <td>69,033,400,000</td> 
       </tr>     
       </tbody> 
      </table> 
      </div> 

     </div> 
    </div> 

は、私が提供する同じ列ヘッダを持つテーブルを再作成する際に特に興味"現在、私は本質的にURLに行き、HTMLをダウンロードし、BeautifulSoupで解析し、次に 'for'ステートメントを使ってtd要素を取得する単純なスクリプトを書くことができました。私のコードのサンプル(URLは省略)と結果の下には:

from bs4 import BeautifulSoup 
import requests 
import pandas as pd 
import csv 

url = "enterURLhere" 
page = requests.get(url) 
pagetext = page.text 

pricetable = { 
    "Date" : [], 
    "Open" : [], 
    "High" : [], 
    "Low" : [], 
    "Close" : [], 
    "Volume" : [], 
    "Market Cap" : [] 
} 

soup = BeautifulSoup(pagetext, 'html.parser') 

file = open("test.csv", 'w') 

for row in soup.find_all('tr'): 
    for col in row.find_all('td'): 
     print(col.text) 

sample output

誰もが少なくともテーブルにデータプルを再フォーマットする方法上の任意のポインタがありますか?ありがとう。

+0

CSVモジュールを見てみましょう:https://docs.python.org/2/library/csv.html – blueCat

答えて

1

コードを実行すると、そのテーブルから目的のデータが得られます。それをやってみると、この非常に要素からデータを抽出するには、あなたがする必要があるすべては私がにコードを破るしようとしたhtml=''' '''

import csv 
from bs4 import BeautifulSoup 

outfile = open("table_data.csv","w",newline='') 
writer = csv.writer(outfile) 

tree = BeautifulSoup(html,"lxml") 
table_tag = tree.select("table")[0] 
tab_data = [[item.text for item in row_data.select("th,td")] 
       for row_data in table_tag.select("tr")] 

for data in tab_data: 
    writer.writerow(data) 
    print(' '.join(data)) 

以内に、あなたは上記貼り付けている全体のhtml要素を、ラップで理解できるようにしてください。私が上で行ったのは、入れ子にされたforループです。ここでは、それは別に行く方法です:

from bs4 import BeautifulSoup 

soup = BeautifulSoup(html,"lxml") 
table = soup.find('table') 

list_of_rows = [] 
for row in table.findAll('tr'): 
    list_of_cells = [] 
    for cell in row.findAll(["th","td"]): 
     text = cell.text 
     list_of_cells.append(text) 
    list_of_rows.append(list_of_cells) 

for item in list_of_rows: 
    print(' '.join(item)) 

結果:

Date Open High Low Close Volume Market Cap 
Sep 14, 2017 3875.37 3920.60 3153.86 3154.95 2,716,310,000 64,191,600,000 
Sep 13, 2017 4131.98 3789.92 3882.59 2,219,410,000 68,432,200,000 
Sep 12, 2017 4168.88 4344.65 4085.22 4130.81 1,864,530,000 69,033,400,000 
+0

私が持っていましたいくつか調整を加えるが、これは完全に機能した。共有してくれてありがとう、私を助ける時間を費やしてくれてありがとう。 論理をどのように記述するかお尋ねしますか? 1. BeautifulSoupはHTMLコンテンツを読み込み可能な形式に引き出します 2.スープ内の最初のテーブルの選択として定義されたtable_tag 3. tab_dataは最初に列見出しで選択されたテキストを取り、row_dataの残りの部分を取り出しますスクリプトが実行されるときにtable_tagから? 4.文章のfor文を説明してください。 ロジックを理解することは非常に役に立ちます。 – user8508478

+0

お寄せいただきありがとうございます。これを答えとしてマークすることを忘れないでください。ありがとう。 – SIM

+0

@ user8508478、更新された回答を参照してください。 – SIM

関連する問題