証券会社のサイトから資産額を取得するコードを参考に、学会の予定を取得するコードを作成しようとしました。
学会開催案内↓
http://www3.imic.or.jp/Scripts/gakkai/ImicInet.dll?OnSearch?iBtnNo=1&hdnSearchData=
証券会社の例ではpd.read_html()で取得したdf_tableのタイプは<class 'pandas.core.frame.DataFrame'>となって扱いやすいのですが、
学会予定の例ではタイプが<class 'list'> (テーブルの内容が全部1つの要素になる)となり、その後の加工法に行き詰まりました。
DataFrameとして扱いたいのですがどうすればよいでしょうか?
<資産取得>
python3
1from selenium import webdriver 2from selenium.webdriver.chrome.options import Options 3from bs4 import BeautifulSoup 4import pandas as pd 5import schedule 6import time 7from datetime import datetime as dt 8import gspread 9import json 10 11def sisankaki(): 12 13 options = Options() 14 # ヘッドレスモード(chromeを表示させないモード) 15 options.add_argument('--headless') 16 17 driver = webdriver.Chrome(executable_path='C:\Users\なまえ\chromedriver.exe', options=options) 18 19 driver.get('https://www.sbisec.co.jp/ETGate') 20 driver.save_screenshot('sbi.png') 21 22 # ユーザーネームとパスワードを設定 23 username = "ゆーざーねーむ" 24 password = "ぱすわーど" 25 26 # ユーザーネームテキストボックスを選択 27 user_id = driver.find_element_by_id("user_input") 28 # ユーザーネームを入力 29 user_id.find_element_by_name("user_id").send_keys(username) 30 # パスワードテキストボックスを選択 31 user_pass = driver.find_element_by_id("password_input") 32 # パスワードを入力 33 user_pass.find_element_by_name("user_password").send_keys(password) 34 # ログインを押下 35 driver.find_element_by_class_name("ov").click() 36 37 # 口座管理に遷移 38 driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/ul/li[3]/a/img").click() 39 #print(driver.current_url) 40 41 # 文字コードをUTF-8に変換 42 html = driver.page_source.encode('utf-8') 43 44 # BeautifulSoupでパース 45 soup = BeautifulSoup(html, "html.parser") 46 47 table = soup.find_all("table", border="0", cellspacing="0", cellpadding="0", width="340") 48 df_table = pd.read_html(str(table))[9] 49 print(df_table) 50 print(type(df_table)) 51 52 ## 必要な行・列のみ抽出(信用ポジがある場合iloc[9,1]にする) 53 sousisan = df_table.iloc[8, 1] 54 55 print(sousisan) 56 57以下略 58
<学会開催日取得>
python3
1from selenium import webdriver 2from selenium.webdriver.chrome.options import Options 3from bs4 import BeautifulSoup 4import pandas as pd 5import schedule 6import time 7from datetime import datetime as dt 8import gspread 9import json 10from time import sleep 11import numpy as np 12 13Keyword = 'きーわーど' 14 15driver = webdriver.Chrome(executable_path='C:\Users\なまえ\chromedriver.exe') 16 17driver.get('http://www3.imic.or.jp/Scripts/gakkai/ImicInet.dll?OnSearch?iBtnNo=1&hdnSearchData=') 18 19# 検索語入力 20driver.find_element_by_xpath('/html/body/form/table/tbody/tr[1]/td[2]/input[1]').send_keys(Keyword) 21 22# 押下 23driver.find_element_by_xpath('/html/body/form/center/input[1]').click() 24sleep(1) 25 26# 文字コードをUTF-8に変換 27html = driver.page_source.encode('utf-8') 28 29# BeautifulSoupでパース 30soup = BeautifulSoup(html, "html.parser") 31 32table = soup.find_all("table") 33df = pd.read_html(str(table)) 34print(df) 35print(type(df))

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/11 01:30