0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

三村かな子をシンデレラガールにするためのデータ戦略(2.5) ー全総選挙データの取り込みー

Last updated at Posted at 2019-01-17

今回は番外編で、第1回から第7回までの総選挙の結果を同じフォーマットのCSVで保存していく。

第1回ではRequestsとbs4を使ってスクレイピングを行ったが、今回は数が多いのとそれぞれのページが微妙に細かく違うのでpandasのpandas.read_htmlメソッドを使って楽させていただいた。
今回は番外編ということで覚書としてコードだけです。
※5回、6回のインデックスの付け方に誤りがあったので修正しました。

#第1回

import pandas as pd

elections = []

# 第1回
URL='https://dic.pixiv.net/a/%E7%AC%AC1%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E9%81%B8%E6%8A%9C%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL,header=0)
df = dfs[0].rename(columns={'順位':'rank'})
df[['name','votes']] = df['名前'].str.split('(',expand=True)
df['votes'] = df['votes'].str.extract('(.+)票')
df['votes'] = df['votes'].str.replace(',','')
df['rank'] = df['rank'].str.replace('','')
df.set_index('name',inplace=True)
df = df[['rank','votes']]
# df.to_csv('data/election1.csv')

elections.append(df)

#第2回

# 第2回
URL='https://dic.pixiv.net/a/%E7%AC%AC2%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E9%81%B8%E6%8A%9C%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL,header=0)
df = dfs[0].rename(columns={'順位':'rank'})
df[['name','votes']] = df['名前'].str.split('',expand=True) #(は全角
df['votes'] = df['votes'].str.extract('(.+)票')
df['votes'] = df['votes'].str.replace(',','')
df['rank'] = df['rank'].str.replace('','')
df.set_index('name',inplace=True)
df = df[['rank','votes']]
# df.to_csv('data/election2.csv')

elections.append(df)

#第3回

# 第3回 ここだけなぜか1回と同じフォーマットのようだ
URL='https://dic.pixiv.net/a/%E7%AC%AC3%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL,header=0)
df = dfs[0].rename(columns={'順位':'rank'})
df[['name','votes']] = df['名前'].str.split('(',expand=True) #(は半角
df['votes'] = df['votes'].str.extract('(.+)票')
df['votes'] = df['votes'].str.replace(',','')
df['rank'] = df['rank'].str.replace('','')
df.set_index('name',inplace=True)
df = df[['rank','votes']]
# df.to_csv('data/election3.csv')

elections.append(df)

#第4回

# 第4回 pixivに票情報がないのでniconicoから抽出
URL='https://dic.nicovideo.jp/a/%E7%AC%AC4%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL)
df = dfs[9]
df = df.iloc[:,-3:]
df.columns=['rank','name','votes']
df.set_index('name',inplace=True)
df['votes'] = df['votes'].str.extract('(.+)票')
df['votes'] = df['votes'].str.replace(',','')
# df.to_csv('data/election4.csv')

elections.append(df)

#第5回

# 第5回
URL='https://dic.pixiv.net/a/%E7%AC%AC5%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL,header=0)
col_names={'順位':'rank','得票数':'votes','名前':'name'}
df = dfs[0].rename(columns=col_names).set_index('name')
df['rank'] = df['rank'].str.replace('','')
df = df[['rank','votes']]
# fifth_df.to_csv('data/election5.csv')

elections.append(df)

#第6回

# 第6回
URL='https://dic.pixiv.net/a/%E7%AC%AC6%E5%9B%9E%E3%82%B7%E3%83%B3%E3%83%87%E3%83%AC%E3%83%A9%E3%82%AC%E3%83%BC%E3%83%AB%E7%B7%8F%E9%81%B8%E6%8C%99'
dfs = pd.read_html(URL,header=0)
col_names = {'Unnamed: 0':'rank','得票数':'votes','名前':'name'}
df = dfs[0].rename(columns=col_names).set_index('name')
df['rank'] = df['rank'].str.replace('','')
df = df[['rank','votes']]
# df.to_csv('data/election6.csv')

elections.append(df)

#第7回

# 第7回(第1回で作ったファイルを使用)
df = pd.read_csv('data/senkyo7.csv').rename(columns={'7th':'rank','votes':'votes'})
df = df.set_index('name')
df = df[['rank','votes']]

# seventh_df.to_csv('data/election7.csv')
elections.append(df)

#確認

# 確認

for d in elections:
    print(d.head(3))

範囲を選択_012.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?