Created
July 4, 2021 15:24
-
-
Save YumNumm/93b5e77eec9dc3e70773bd92199b0a08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
""" | |
都議会議員選挙2021の議席状況を取得して、円グラフを描画する | |
Seleniumで強引に取ってきてるので、時間がかかる。 | |
TODO: BeautifulSoupでスクレイピングしてみる | |
ちなみに、東京議会選挙は英語で"Tokyo Metropolitan Assembly Election"らしい。 | |
""" | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
l = [] | |
label = [] | |
chrome_option = webdriver.ChromeOptions() | |
chrome_option.add_argument("--headless") | |
chrome_option.add_argument("--no-sandbox") | |
chrome_option.add_argument("--disable-setuid-sandbox") | |
driver = webdriver.Chrome( | |
executable_path="/usr/lib/chromium-browser/chromedriver", | |
options=chrome_option, | |
) | |
driver.get("https://www.nhk.or.jp/senkyo/database/togisen/2021/") | |
for i in range(1, 9, 1): | |
l.append( | |
driver.find_element( | |
By.CSS_SELECTOR, | |
".mainGraph__party__item:nth-child(" | |
+ str(i) | |
+ ") .mainGraph__party__count", | |
).text | |
) | |
label.append( | |
driver.find_element( | |
By.CSS_SELECTOR, | |
".mainGraph__party__item:nth-child(" | |
+ str(i) | |
+ ") .mainGraph__party__name", | |
).text | |
) | |
driver.quit() | |
x = np.array(l) | |
print(l) | |
print(label) | |
plt.rcParams["font.family"] = "Droid Sans Fallback" | |
fig = plt.figure() | |
plt.pie(x, labels=label, textprops={"weight": "bold"}) | |
fig.savefig("pie.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment