1
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 1 year has passed since last update.

Python、selenium、webdriverでログイン後の画面の要素をスクレイピング(仮)

Posted at

前提

$ python --version
Python 3.9.0
$ pip list
Package          Version
---------------- ---------
pandas           1.5.1
selenium         4.5.0

コード

scraping.py
from selenium.webdriver.common.by import By
from selenium import webdriver
import pandas as pd
import csv

USER = 'メールアドレスとかIDとか'
PASS = 'パスワード'

browser = webdriver.Chrome()
# ログイン画面
browser.get('ログイン画面のURL')

# ID,PW,送信ボタン
elem_username = browser.find_element(By.CSS_SELECTOR, "#ID名やClass名など")
elem_password = browser.find_element(By.CSS_SELECTOR, "#ID名やClass名など")
browser_from = browser.find_element(By.CSS_SELECTOR, "#ID名やClass名など")
# 値を入力し、送信ボタンを押下
elem_username.send_keys(USER)
elem_password.send_keys(PASS)
browser_from.click()

# 取得したい文字列の要素
elm = browser.find_element(By.CSS_SELECTOR, "#ID名やClass名など")
# 要素のテキスト内容を取得
num = [elm.text]
header = ["タイトル"]
df = pd.DataFrame({
  '成果':num
}, index = header)
df.to_csv("test.csv",encoding="UTF-8")

# Webdriverを閉じる
browser.close()

実行

$ python scraping.py

実行後

test.csv というファイルが、実行した scraping.py と同じディレクトリに生成される

内容

test.csv
,成果
タイトル,取得したテキスト内容

今後

  • pandasの使い方
  • pythonでのHTML要素の取得方法
  • 実行後にページ遷移して、さらに同様にCSVにデータを入れる

参考

1
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
1
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?