質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

6364閲覧

pandasでhtmlからテーブルを取得する

DaichiYasuda

総合スコア173

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2017/12/10 14:42

あるサイトのhtmlからテーブルをpandasで抜き出したい。

python

1from selenium import webdriver 2import pandas as pd 3import requests 4from bs4 import BeautifulSoup 5 6driver = webdriver.PhantomJS() 7driver.set_window_size(1024, 768) 8 9#ここの処理は省きます 10 11url = driver.current_url 12 13data = driver.page_source.encode() 14soup = BeautifulSoup(data, 'html.parser') 15 16#tableを取得 17tables = soup.find_all('table', attrs={'class': 'pro-table pro-table02 mb5 tableCross_1'}) 18 19dflist = pd.io.html.read_html(str(tables))

tablesの中身(googledoc)

dflistの中身(googledoc)

csvファイルの完成形(googleスプレッドシート)

最終的にはcsvファイルの完成形(googleスプレッドシート)のような表にしたのですができますでしょうか?

どなたかご教示お願い致します

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

dflistにてpd.DataFrameがうまく取得できているように見えます。
となるとあとは列の名前の変更と行インデックスの指定でしょうか。

列名前変更
http://nehan.io/blog/python-data-transform/id-10

特定の列をインデックスに指定
http://nekoyukimmm.hatenablog.com/entry/2016/02/25/094407

最後にCSVファイルに出力
https://qiita.com/ryu19maki/items/e5a3b470795de883a09a
https://pythondatascience.plavox.info/pandas/データフレームを出力する


手元では特に問題なくCSVファイルにできます。

python

1import pandas as pd 2from bs4 import BeautifulSoup 3 4a = ''' 5<table class="pro-table pro-table02 mb5 tableCross_1"> 6<thead> 7<tr> 8<th class="w7p" rowspan="2"> </th> 9<th class="w5p" rowspan="2">合計</th> 10<th colspan="9">内訳</th> 11<th class="w5p area-disabled" rowspan="2"><a class="gonFloat8 cboxElement" href="#print">地図・クーポン印刷</a></th> 12<th class="w5p area-disabled" rowspan="2">チョットぐに追加</th> 13<th class="w5p area-disabled" rowspan="2">口コミ</th> 14<th class="w5p area-disabled" rowspan="2">お店のブログ</th> 15</tr> 16<tr> 17<th class="w5p">店舗トップ</th> 18<th class="w5p"><a class="gonFloat cboxElement" href="#menu">メニュー</a></th> 19<th class="w5p"><a class="gonFloat5 cboxElement" href="#seat">席・個室・貸切</a></th> 20<th class="w5p"><a class="gonFloat2 cboxElement" href="#photo">写真</a></th> 21<th class="w5p">こだわり</th> 22<th class="w5p"><a class="gonFloat7 cboxElement" href="#map">地図</a></th> 23<th class="w5p">クーポン</th> 24<th class="w5p"><a class="gonFloat3 cboxElement" href="#yoyaku">予約</a></th> 25<th class="w5p"><a class="gonFloat6 cboxElement" href="#other">その他</a></th> 26</tr> 27</thead> 28<tbody> 29<tr> 30<th class="txtLeft">2016/11</th> 31<td class="txtRight table1item1">8158</td> 32<td class="txtRight table1item2">2740</td> 33<td class="txtRight table1item3">2199</td> 34<td class="txtRight table1item4">998</td> 35<td class="txtRight table1item5">420</td> 36<td class="txtRight table1item6">214</td> 37<td class="txtRight table1item7">542</td> 38<td class="txtRight table1item8">242</td> 39<td class="txtRight table1item9">790</td> 40<td class="txtRight table1item10">13</td> 41<td class="txtRight table1item11">49</td> 42<td class="txtRight table1item12">0</td> 43<td class="txtRight table1item13">111</td> 44<td class="txtRight table1item14">0</td> 45</tr> 46<tr> 47<th class="txtLeft">2016/12</th> 48<td class="txtRight table1item1">5401</td> 49<td class="txtRight table1item2">1909</td> 50<td class="txtRight table1item3">1255</td> 51<td class="txtRight table1item4">510</td> 52<td class="txtRight table1item5">360</td> 53<td class="txtRight table1item6">140</td> 54<td class="txtRight table1item7">562</td> 55<td class="txtRight table1item8">139</td> 56<td class="txtRight table1item9">515</td> 57<td class="txtRight table1item10">11</td> 58<td class="txtRight table1item11">75</td> 59<td class="txtRight table1item12">0</td> 60<td class="txtRight table1item13">67</td> 61<td class="txtRight table1item14">0</td> 62</tr> 63<tr> 64<th class="txtLeft">2017/01</th> 65<td class="txtRight table1item1">4584</td> 66<td class="txtRight table1item2">1534</td> 67<td class="txtRight table1item3">1219</td> 68<td class="txtRight table1item4">413</td> 69<td class="txtRight table1item5">341</td> 70<td class="txtRight table1item6">137</td> 71<td class="txtRight table1item7">333</td> 72<td class="txtRight table1item8">135</td> 73<td class="txtRight table1item9">460</td> 74<td class="txtRight table1item10">12</td> 75<td class="txtRight table1item11">18</td> 76<td class="txtRight table1item12">0</td> 77<td class="txtRight table1item13">49</td> 78<td class="txtRight table1item14">0</td> 79</tr> 80<tr> 81<th class="txtLeft">2017/02</th> 82<td class="txtRight table1item1">4821</td> 83<td class="txtRight table1item2">1711</td> 84<td class="txtRight table1item3">1243</td> 85<td class="txtRight table1item4">407</td> 86<td class="txtRight table1item5">335</td> 87<td class="txtRight table1item6">134</td> 88<td class="txtRight table1item7">299</td> 89<td class="txtRight table1item8">169</td> 90<td class="txtRight table1item9">513</td> 91<td class="txtRight table1item10">10</td> 92<td class="txtRight table1item11">22</td> 93<td class="txtRight table1item12">0</td> 94<td class="txtRight table1item13">60</td> 95<td class="txtRight table1item14">0</td> 96</tr> 97<tr> 98<tr class="total"> 99<th class="txtLeft">合計</th> 100<td class="txtRight w100 table1item1">59010</td> 101<td class="txtRight w100 table1item2">19188</td> 102<td class="txtRight w100 table1item3">14620</td> 103<td class="txtRight w100 table1item4">5547</td> 104<td class="txtRight w100 table1item5">3837</td> 105<td class="txtRight w100 table1item6">1584</td> 106<td class="txtRight w100 table1item7">3993</td> 107<td class="txtRight w100 table1item8">1521</td> 108<td class="txtRight w100 table1item9">8615</td> 109<td class="txtRight w100 table1item10">105</td> 110<td class="txtRight w100 table1item11">347</td> 111<td class="txtRight w100 table1item12">0</td> 112<td class="txtRight w100 table1item13">664</td> 113<td class="txtRight w100 table1item14">0</td> 114</tr> 115</tbody> 116</table> 117''' 118 119dfs = pd.io.html.read_html(a) 120df = dfs[0] 121 122df.columns = ['date', '合計', '店舗トップ', 'メニュー', 123 '席・個室・貸切', '写真', 'こだわり', '地図', 124 'クーポン', '予約', 'その他', '地図・クーポン印刷', 125 'チョットぐに追加', '口コミ', 'お店のブログ'] 126 127df = df.set_index('date') 128del df.index.name 129 130df.to_csv('mydata.csv')

投稿2017/12/11 00:26

編集2017/12/12 04:49
mkgrei

総合スコア8560

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

DaichiYasuda

2017/12/11 14:26

thタグが復数に別れておりまして、インデックスの指定ができません。 助けていただいてもよろしいでしょうか?
mkgrei

2017/12/12 04:50

リストからデータフレームを取り出していますか? それでもインデックス指定ができませんか?
DaichiYasuda

2017/12/17 14:43

ありがとうございました! データフレーム取り出して解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問