from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from bs4 import BeautifulSoup from google.colab import auth from oauth2client.client import GoogleCredentials import gspread import time import requests import os #Urlに飛ぶ url = '*****************************' driver = webdriver.Chrome('chromedriver',options=options) driver.get(url) soup = BeautifulSoup(driver.page_source, 'html5lib') #ブラウザ操作 username = driver.find_element_by_name('username').send_keys("**********") password = driver.find_element_by_name('password').send_keys("**********") driver.find_element_by_class_name('login_button').click() e = driver.find_element(By.CSS_SELECTOR,"#container tr:nth-child(1) td:nth-child(3) a") link=e.get_attribute("href") driver.get(link) e = driver.find_element(By.CSS_SELECTOR,"#container a:nth-child(3)") link=e.get_attribute("href") driver.get(link) print(driver.current_url) es = driver.find_elements(By.CSS_SELECTOR,"#container > form > table.list > tbody > tr") ds =[] for d in es: if d.text.startswith("["): r = len(d.text) da = d.text ds.append(da) time.sleep(3) #スプシを開く auth.authenticate_user() gc = gspread.authorize(GoogleCredentials.get_application_default()) ss_id = "************************************" workbook = gc.open_by_key(ss_id) print(workbook.title) ws = workbook.worksheet('シート1') #スプシへの書き出し a = 1 b = 1 cell_list = ('B2:G' + str(len(d.text))) for i in cell_list: i = ds[a][b] j = int(i) if b == 8: b = 1 b += 1 else: b += 1 if a == len(d.text)-1: break ws.update_cell(j, 1, ds[j]) driver.quit
■上記のコードを組んだ所、スプレッドシートへの書き出しだうまくいかず、
A7セルに値が1つのみ記載されてしまいます。
希望としてはB2:G〇の特定範囲にデータをを分けて書き出しを希望しております。
■書き出しイメージ
A B C D E F G
1
2 [011111] ●● ●● 社員( 2021/03/29 10:17:31 2021/03/29 有給休暇 瞼が腫れてし..
3 [011111] ●● ●● 社員( 2021/03/29 10:17:31 2021/03/29 有給休暇 瞼が腫れてし..
4 [011111] ●● ●● 社員( 2021/03/29 10:17:31 2021/03/29 有給休暇 瞼が腫れてし..
5 [011111] ●● ●● 社員( 2021/03/29 10:17:31 2021/03/29 有給休暇 瞼が腫れてし..
6 ............................................................................
あなたの回答
tips
プレビュー