0
0

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.

アンケートを自動化しよう!!

Last updated at Posted at 2022-11-21

私の学校では、履修登録をする為にアンケートを回答する必要があります。
それが正直、、、メンドクサイ

注意:

  • Webスクレイピングを用いるのですが

    場合によっては、違法になります!!
    サイトの利用規約やrobots.txtをしっかりと確認してください!!
    使用する際は、あくまでも自己責任でお願いいたします。

  • 全てのアンケートに対応するプログラムではありません。
    自動で回答したいアンケートに合わせてプログラムを作成してください。

今回、回答するアンケートはこちら、、、(スクレイピング用に用意しました。)

screencapture-file-C-Users-81909-Desktop-sousei-html-2022-11-21-14_35_55.png

HTML

<!DOCTYPE html>
<body style="background-color:rgb(181, 247, 181);">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>アンケート</title>
</head>

<form method="post" action="example.cgi">


<p><h1 align="center">授業アンケート</h1>


<p><h3>アンケート評価方法</p>
    <p>1:ダメ、いいえ<br>
    2:どちらかと言えばダメ、どちらかと言えばいいえ<br>
    3:普通<br>
    4:どちらかと言えば良い、どちらかと言えばはい<br>
    5:良い、はい<br></p>
    <body>

    <p>(1)今学期の授業の理解度は?<br>
        <input type="radio" name="1" value="1">1 
        <input type="radio" name="1" value="2">2
        <input type="radio" name="1" value="3">3
        <input type="radio" name="1" value="4">4
        <input type="radio" name="1" value="5">5
<br>

<body>
    <p>(2)今学期の出席率はよかったか?<br>
        <input type="radio" name="2" value="1">1 
        <input type="radio" name="2" value="2">2
        <input type="radio" name="2" value="3">3
        <input type="radio" name="2" value="4">4
        <input type="radio" name="2" value="5">5
<br>

<label for="message">理由</label><br>
<textarea name="message_1" rows="10" cols="80" id="message_1"></textarea>
<br>

<body>
    <p>(3)今学期の単位の取得率はよかったか?<br>
        <input type="radio" name="3" value="1">1 
        <input type="radio" name="3" value="2">2
        <input type="radio" name="3" value="3">3
        <input type="radio" name="3" value="4">4
        <input type="radio" name="3" value="5">5
<br>

<label for="message">理由</label><br>
<textarea name="message_2" rows="10" cols="80" id="message"></textarea>
<br>

<body>
    <p>(4)授業に対して予習と復習を積極的に行いましたか?<br>
        <input type="radio" name="4" value="1">1 
        <input type="radio" name="4" value="2">2
        <input type="radio" name="4" value="3">3
        <input type="radio" name="4" value="4">4
        <input type="radio" name="4" value="5">5
<br>

<body>
    <p>(5)授業に対して意欲的に取り組みましたか?<br>
        <input type="radio" name="5" value="1">1 
        <input type="radio" name="5" value="2">2
        <input type="radio" name="5" value="3">3
        <input type="radio" name="5" value="4">4
        <input type="radio" name="5" value="5">5
<br>

<body>
    <p>(6)授業を理解するために努力したか?<br>
        <input type="radio" name="6" value="1">1 
        <input type="radio" name="6" value="2">2
        <input type="radio" name="6" value="3">3
        <input type="radio" name="6" value="4">4
        <input type="radio" name="6" value="5">5
<br>

<body>
    <p>(7)学生が質問や意見を述べられるように配慮されていたか?<br>
        <input type="radio" name="7" value="1">1 
        <input type="radio" name="7" value="2">2
        <input type="radio" name="7" value="3">3
        <input type="radio" name="7" value="4">4
        <input type="radio" name="7" value="5">5
<br>

<body>
    <p>(8)授業の内容は興味や関心が持てるものでしたか?<br>
        <input type="radio" name="8" value="1">1 
        <input type="radio" name="8" value="2">2
        <input type="radio" name="8" value="3">3
        <input type="radio" name="8" value="4">4
        <input type="radio" name="8" value="5">5
<br>

<body>
    <p>(9)配布資料などはわかりやすかったか?<br>
        <input type="radio" name="9" value="1">1 
        <input type="radio" name="9" value="2">2
        <input type="radio" name="9" value="3">3
        <input type="radio" name="9" value="4">4
        <input type="radio" name="9" value="5">5
<br>

<body>
    <p>(10)シラバスで示された到達目標が達成されるように、授業が行われたか?<br>
        <input type="radio" name="10" value="1">1 
        <input type="radio" name="10" value="2">2
        <input type="radio" name="10" value="3">3
        <input type="radio" name="10" value="4">4
        <input type="radio" name="10" value="5">5
<br>

<body>
    <p>(11)総合的にみて授業は有意義だったか?<br>
        <input type="radio" name="11" value="1">1 
        <input type="radio" name="11" value="2">2
        <input type="radio" name="11" value="3">3
        <input type="radio" name="11" value="4">4
        <input type="radio" name="11" value="5">5
<br>

<label for="message">理由</label><br>
<textarea name="message_3" rows="10" cols="80" id="message"></textarea>
<br>

<body>
    <p>(12)授業の目的、到達目標、求められた課題について把握していたか?<br>
        <input type="radio" name="12" value="1">1 
        <input type="radio" name="12" value="2">2
        <input type="radio" name="12" value="3">3
        <input type="radio" name="12" value="4">4
        <input type="radio" name="12" value="5">5
<br>

<body>
    <p>(13)授業の内容は理解できるものか。?<br>
        <input type="radio" name="13" value="1">1 
        <input type="radio" name="13" value="2">2
        <input type="radio" name="13" value="3">3
        <input type="radio" name="13" value="4">4
        <input type="radio" name="13" value="5">5
<br>

<body>
    <p>(14)時間配分など、授業の進め方は適切でしたか?<br>
        <input type="radio" name="14" value="1">1 
        <input type="radio" name="14" value="2">2
        <input type="radio" name="14" value="3">3
        <input type="radio" name="14" value="4">4
        <input type="radio" name="14" value="5">5
<br>

<body>
    <p>(15)板書やOHC、パワーポイントの字や図の表現は適切でしたか?<br>
        <input type="radio" name="15" value="1">1 
        <input type="radio" name="15" value="2">2
        <input type="radio" name="15" value="3">3
        <input type="radio" name="15" value="4">4
        <input type="radio" name="15" value="5">5
<br>

<body>
    <p>(16)学生が質問や意見を述べられるように配慮していたか?<br>
        <input type="radio" name="16" value="1">1 
        <input type="radio" name="16" value="2">2
        <input type="radio" name="16" value="3">3
        <input type="radio" name="16" value="4">4
        <input type="radio" name="16" value="5">5
<br>

<body>
    <p>(17)後輩にも是非すすめたい授業ですか?<br>
        <input type="radio" name="17" value="1">1 
        <input type="radio" name="17" value="2">2
        <input type="radio" name="17" value="3">3
        <input type="radio" name="17" value="4">4
        <input type="radio" name="17" value="5">5
<br>

<body>
    <p>(18)指定教科書は、この授業に適切なものでしたか?<br>
        <input type="radio" name="18" value="1">1 
        <input type="radio" name="18" value="2">2
        <input type="radio" name="18" value="3">3
        <input type="radio" name="18" value="4">4
        <input type="radio" name="18" value="5">5
<br>

<body>
    <p>(19)学生からの反応や意見を生かした授業であったか?<br>
        <input type="radio" name="19" value="1">1 
        <input type="radio" name="19" value="2">2
        <input type="radio" name="19" value="3">3
        <input type="radio" name="19" value="4">4
        <input type="radio" name="19" value="5">5
<br>

<body>
    <p>(20)講義はよく聞き取れたか?<br>
        <input type="radio" name="20" value="1">1 
        <input type="radio" name="20" value="2">2
        <input type="radio" name="20" value="3">3
        <input type="radio" name="20" value="4">4
        <input type="radio" name="20" value="5">5
<br>

<body>
    <p>(21)授業の進度や時間配分は適切であったか?<br>
        <input type="radio" name="21" value="1">1 
        <input type="radio" name="21" value="2">2
        <input type="radio" name="21" value="3">3
        <input type="radio" name="21" value="4">4
        <input type="radio" name="21" value="5">5
<br>

<body>
    <p>(22)教員の熱意や意欲を感じたか?<br>
        <input type="radio" name="22" value="1">1 
        <input type="radio" name="22" value="2">2
        <input type="radio" name="22" value="3">3
        <input type="radio" name="22" value="4">4
        <input type="radio" name="22" value="5">5
<br>

<body>
    <p>(23)授業内容の分量は適切であったか?<br>
        <input type="radio" name="23" value="1">1 
        <input type="radio" name="23" value="2">2
        <input type="radio" name="23" value="3">3
        <input type="radio" name="23" value="4">4
        <input type="radio" name="23" value="5">5
<br>

<body>
    <p>(24)授業で取り上げられた事柄は興味ある内容であったか?<br>
        <input type="radio" name="24" value="1">1 
        <input type="radio" name="24" value="2">2
        <input type="radio" name="24" value="3">3
        <input type="radio" name="24" value="4">4
        <input type="radio" name="24" value="5">5
<br>

<body>
    <p>(25)の授業で学んだことは今後役に立つと思ったか?<br>
        <input type="radio" name="25" value="1">1 
        <input type="radio" name="25" value="2">2
        <input type="radio" name="25" value="3">3
        <input type="radio" name="25" value="4">4
        <input type="radio" name="25" value="5">5
<br>

<body>
    <p>(26)総合的に評価して、あなたは授業に満足しましたか?<br>
        <input type="radio" name="26" value="1">1 
        <input type="radio" name="26" value="2">2
        <input type="radio" name="26" value="3">3
        <input type="radio" name="26" value="4">4
        <input type="radio" name="26" value="5">5
<br>

<body>
    <p>(27)担当教員は、具体例を挙げたり等分かりやすく説明をしていたか?<br>
        <input type="radio" name="27" value="1">1 
        <input type="radio" name="27" value="2">2
        <input type="radio" name="27" value="3">3
        <input type="radio" name="27" value="4">4
        <input type="radio" name="27" value="5">5
<br>

<body>
    <p>(28)事前に教科書や資料を読むなど、十分に準備をして授業を受けたか?<br>
        <input type="radio" name="28" value="1">1 
        <input type="radio" name="28" value="2">2
        <input type="radio" name="28" value="3">3
        <input type="radio" name="28" value="4">4
        <input type="radio" name="28" value="5">5
<br>

<body>
    <p>(29)遅刻や欠席をしなかったか?<br>
        <input type="radio" name="29" value="1">1 
        <input type="radio" name="29" value="2">2
        <input type="radio" name="29" value="3">3
        <input type="radio" name="29" value="4">4
        <input type="radio" name="29" value="5">5
<br>

<body>
    <p>(30)全体として、積極的に授業に参加することができたか?<br>
        <input type="radio" name="30" value="1">1 
        <input type="radio" name="30" value="2">2
        <input type="radio" name="30" value="3">3
        <input type="radio" name="30" value="4">4
        <input type="radio" name="30" value="5">5
<br>

<body>
    <p>(31)私語、携帯電話、途中入退場のマナーなどに気を配ったか?<br>
        <input type="radio" name="31" value="1">1 
        <input type="radio" name="31" value="2">2
        <input type="radio" name="31" value="3">3
        <input type="radio" name="31" value="4">4
        <input type="radio" name="31" value="5">5
<br>

<body>
    <p>(32)担当教員は、成績の評価方法を説明したか?<br>
        <input type="radio" name="32" value="1">1 
        <input type="radio" name="32" value="2">2
        <input type="radio" name="32" value="3">3
        <input type="radio" name="32" value="4">4
        <input type="radio" name="32" value="5">5
<br>

<body>
    <p>(33)授業の開始、終了時刻は守られていたか?<br>
        <input type="radio" name="33" value="1">1 
        <input type="radio" name="33" value="2">2
        <input type="radio" name="33" value="3">3
        <input type="radio" name="33" value="4">4
        <input type="radio" name="33" value="5">5
<br>

<body>
    <p>(34)全体として、適度の緊張感と望ましいマナーで気持ちよく授業を受けることができたか?<br>
        <input type="radio" name="34" value="1">1 
        <input type="radio" name="34" value="2">2
        <input type="radio" name="34" value="3">3
        <input type="radio" name="34" value="4">4
        <input type="radio" name="34" value="5">5
<br>

<body>
    <p>(35)担当教員は、学生の理解度に応じた課題を出すなど、学生が自ら学ぶことを促したか?<br>
        <input type="radio" name="35" value="1">1 
        <input type="radio" name="35" value="2">2
        <input type="radio" name="35" value="3">3
        <input type="radio" name="35" value="4">4
        <input type="radio" name="35" value="5">5
<br>

<body>
    <p>(36)担当教員は、学生一人一人を尊重し、学生同士も尊重し合える雰囲気作りを心がけたか?<br>
        <input type="radio" name="36" value="1">1 
        <input type="radio" name="36" value="2">2
        <input type="radio" name="36" value="3">3
        <input type="radio" name="36" value="4">4
        <input type="radio" name="36" value="5">5
<br>

<body>
    <p>(37)担当教員は、各授業および授業全体の目的を明確に示していたか?<br>
        <input type="radio" name="37" value="1">1 
        <input type="radio" name="37" value="2">2
        <input type="radio" name="37" value="3">3
        <input type="radio" name="37" value="4">4
        <input type="radio" name="37" value="5">5
<br>


<body>
    <p>(38)教員は学生の質問・発言等に適した対応をしたか?<br>
        <input type="radio" name="38" value="1">1 
        <input type="radio" name="38" value="2">2
        <input type="radio" name="38" value="3">3
        <input type="radio" name="38" value="4">4
        <input type="radio" name="38" value="5">5
<br>

<body>
    <p>(39)授業で興味深い、面白いと感じたことがありましたか。?<br>
        <input type="radio" name="39" value="1">1 
        <input type="radio" name="39" value="2">2
        <input type="radio" name="39" value="3">3
        <input type="radio" name="39" value="4">4
        <input type="radio" name="39" value="5">5
<br>


<body>
    <p>(40)授業により知的に刺激され,さらに深く勉強したくなったか?<br>
        <input type="radio" name="40" value="1">1 
        <input type="radio" name="40" value="2">2
        <input type="radio" name="40" value="3">3
        <input type="radio" name="40" value="4">4
        <input type="radio" name="40" value="5">5
<br>

<body>
    <p>(41)教員は効果的に学生の参加(発言、自主的学習、作業など)を促したか?<br>
        <input type="radio" name="41" value="1">1 
        <input type="radio" name="41" value="2">2
        <input type="radio" name="41" value="3">3
        <input type="radio" name="41" value="4">4
        <input type="radio" name="41" value="5">5
<br>

<body>
    <p>(42)コミュニケーション能力は向上したか?<br>
        <input type="radio" name="42" value="1">1 
        <input type="radio" name="42" value="2">2
        <input type="radio" name="42" value="3">3
        <input type="radio" name="42" value="4">4
        <input type="radio" name="42" value="5">5
<br>

<body>
    <p>(43)教室内の学習環境は適切に保たれていたか?<br>
        <input type="radio" name="43" value="1">1 
        <input type="radio" name="43" value="2">2
        <input type="radio" name="43" value="3">3
        <input type="radio" name="43" value="4">4
        <input type="radio" name="43" value="5">5
<br>

<body>
    <p>(44)授業を受けて知的好奇心を刺激されたか?<br>
        <input type="radio" name="44" value="1">1 
        <input type="radio" name="44" value="2">2
        <input type="radio" name="44" value="3">3
        <input type="radio" name="44" value="4">4
        <input type="radio" name="44" value="5">5
<br>

<body>
    <p>(45)授業を受けて知識が深まり、あるいは能力が高まったか?<br>
        <input type="radio" name="45" value="1">1 
        <input type="radio" name="45" value="2">2
        <input type="radio" name="45" value="3">3
        <input type="radio" name="45" value="4">4
        <input type="radio" name="45" value="5">5
<br>

<body>
    <p>(46)課題の量や内容はちょうどよかったか?<br>
        <input type="radio" name="46" value="1">1 
        <input type="radio" name="46" value="2">2
        <input type="radio" name="46" value="3">3
        <input type="radio" name="46" value="4">4
        <input type="radio" name="46" value="5">5
<br>

<body>
    <p>(47)教員は授業を飽きさせない創意工夫を行っていましたか?<br>
        <input type="radio" name="47" value="1">1 
        <input type="radio" name="47" value="2">2
        <input type="radio" name="47" value="3">3
        <input type="radio" name="47" value="4">4
        <input type="radio" name="47" value="5">5
<br>

<body>
    <p>(48)あなたは授業を通して自分にとって新しい考え方や発想が身につきましたか?<br>
        <input type="radio" name="48" value="1">1 
        <input type="radio" name="48" value="2">2
        <input type="radio" name="48" value="3">3
        <input type="radio" name="48" value="4">4
        <input type="radio" name="48" value="5">5
<br>

<body>
    <p>(49)あなたは授業を通して自分で調べ、考える姿勢などが身につきましたか?<br>
        <input type="radio" name="49" value="1">1 
        <input type="radio" name="49" value="2">2
        <input type="radio" name="49" value="3">3
        <input type="radio" name="49" value="4">4
        <input type="radio" name="49" value="5">5
<br>

<body>
    <p>(50)あなたは授業に積極的に参加(質問、発言など)していましたか?<br>
        <input type="radio" name="50" value="1">1 
        <input type="radio" name="50" value="2">2
        <input type="radio" name="50" value="3">3
        <input type="radio" name="50" value="4">4
        <input type="radio" name="50" value="5">5
<br>


<p>
   <button type="submit" name="btn_confirm">入力内容を確認する</button>
</p>

<p>
<input type="submit">
<input type="reset">
</p>

</body>
</html>

webスクレイピングを行う為に、
seleniumというツールを使う

下記コマンドをターミナルに入力してseleniumをインストールする

pip install selenium

seleniumをインポートする

# Selenium WebDriver: webブラウザをプログラムから自動的に操作するためのツール
from selenium imoport webdriver

1 アンケートサイトURL , 質問の数 , 記述式の数を保持する変数を定義する

(後で、使います。)

print("アンケートのURLを入力してください。")
url = input()

print("アンケートの選択肢の数を入力してください")
questions = input()

print("記述式の個数を入力してください。")
text = input()

input関数: ユーザーが入力したデータをキーボードに受け付ける為の関数

2 PythonでGoogleChromを立ち上げよう!

ChromeDriverをwindows10でインストールする方法【画像付き】

こちらのサイトを参考に、
chromedriverをインストールしてください

Pythonでchromedriverを立ち上げる

browser = webdriver.Chrome('chromedriver.exe')

3 アンケートサイトを立ち上げよう!

urlを指定して、browser.get(url)で
webサイトを立ち上げる事が出来ます。


url = '自分が立ち上げたいサイトのURL'
browser.get(url)

# => 自分が立ち上げたいサイトを立ち上げる事が可能

今回の場合、input関数で事前にURLを定義してもらい、
入力してもらった、サイトを立ち上げるようにしています!

print("アンケートのURLを入力してください。")
url = input()

browser.get(url)

4 Pythonを使って、ラジオボタンのアンケートを回答しよう!!

(1)アンケートサイトでラジオボタンでの解答の位置をデベロッパーツールで確認しよう

<input type="radio" name="2" value="1">1 
<input type="radio" name="2" value="2">2
<input type="radio" name="2" value="3">3
<input type="radio" name="2" value="4">4
<input type="radio" name="2" value="5">5

type = radio , name = 2 , value = 1 ~ 5の位置となっている

全てのアンケートは、typeがradio , nameが1 ~ 50 , valueがそれぞれ1~5となっている
nameが1 ~ 50 でfor文で周す。

for i in range(1 , int(questions)+1):

    # answerという変数に対して、1~5の数字を入れる
    answer = random.randint(1,5)
    
    #iの値は、1,2,3,4,5....となり、@name = 1 @name=2と変化する
    question = browser.find_elements_by_xpath(f"//input[@name={i}][@type='radio']")

    radio_arr = {}

    for elem in question:
        
        # answerには、1~5のいずれかの数字が入る => ランダムで回答可能
        if elem.get_attribute("value") == str(answer):
            
            #valueの値がstr(answer)となる選択肢をクリックする
            elem.click()

記述式の回答:

for j in range(1, int(text)+1):

    # for文で、message_1 , message_2 , message_3...という位置を指定
    question_2 = browser.find_element_by_name(f"message_{j}")

    # 指定した位置にsend_keysに文字入力
    question_2.send_keys("特になし")

ちなみに、、、

チェックボックス(labelタグに含まれる場合)

question_6 = browser.find_elements_by_xpath("//input[@name='q007'][@type='checkbox']")
labels = browser.find_elements_by_xpath("//label")
checkbox_arr = {}
for elem in question_6:
    for label in labels:
        if (elem.get_attribute("id") == label.get_attribute("for")):  
            checkbox_arr[elem.get_attribute("value")] = label.text
            if elem.get_attribute("value") == "001":
                label.click()    

参考文献:
http://holiday-programmer.net/selenium-radio/
https://www.bing.com/search?q=selenium+チェックボックス+テスト&cvid=bfbf4a357b624aa4a64601c3390dec61&aqs=edge..69i57.13822j0j1&pglt=299&FORM=ANNTA1&PC=U531
https://self-development.info/python%e3%81%a7chromedriver%e3%81%ab%e3%82%88%e3%82%8aselenium%e6%93%8d%e4%bd%9c/
https://hibiki-press.tech/python/webbrowser_module/1884
https://programming-dojo.com/%E3%80%90python3%E5%85%A5%E9%96%80%E3%80%91%E6%96%87%E5%AD%97%E5%88%97%E3%81%AE%E4%B8%AD%E3%81%AB%E5%A4%89%E6%95%B0%E5%B1%95%E9%96%8B%E3%82%92%E3%81%99%E3%82%8B/
https://www.headboost.jp/python-convert-int-into-str/

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?