1
1

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 5 years have passed since last update.

初めてのモバイルアプリ開発 ~仕様説明編~

Posted at

シリーズ目次

仕様説明

今回は、下の本を参考に 技術士試験問題のクイズアプリ を 作成してみようと思います。

Ionicで作る モバイルアプリ制作入門
image.png

クイズを集める

問題文をスクレイピングする

問題分は ここから拝借します。
技術士試験ナビ

jupyter
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
jupyter
browser = webdriver.Chrome('chromedriver.exe')
browser.set_page_load_timeout(30)
jupyter
def getQuestion(entry_content):
    entry_list = entry_content.find_elements_by_tag_name("p")
    question = ""
    anser = ""
    choice = []
    image = []
    for p in entry_list:
        img = p.find_elements_by_tag_name("img")
        span = p.find_elements_by_tag_name("span")
        name = p.get_attribute('class')

        if len(img) > 0:
            # 画像
            for i in img:
                src = i.get_attribute('src')
                image.append(src)
        elif len(span) > 0:
            anser = span[0].text # 答え
        else:
            if name == "":
                if question == "":
                    question = p.text
            else:
                choice.append(p.text)
                
    return question, anser, choice, image
jupyter
question_csv = []
anser_csv = []
choice_csv = []
image_csv = []

base_url = 'https://pe.hpeo.jp/entry/pe/1k/{}'

for i in range (2111, 2144):
    url = base_url.format(i)
    browser.get(url)
    entry_content = browser.find_element_by_class_name("entry-content")
    h1 = entry_content.find_elements_by_tag_name("h1")
    if len(h1) > 0:
        continue
    question, anser, choice, image = getQuestion(entry_content)

    question_csv.append([i,question])
    anser_csv.append([i,anser])
    choice.insert(0, i)
    choice_csv.append(choice)
    image.insert(0, i)
    image_csv.append(image)
jupyter
# ファイルに書き込む
import csv

with open('question.csv', 'w') as f:
    writer = csv.writer(f, lineterminator='\n')
    for row in question_csv:
        writer.writerow(row)
        
with open('anser.csv', 'w') as f:
    writer = csv.writer(f, lineterminator='\n')
    for row in anser_csv:
        writer.writerow(row)
        
with open('choice.csv', 'w') as f:
    writer = csv.writer(f, lineterminator='\n')
    for row in choice_csv:
        writer.writerow(row)
        
with open('image.csv', 'w') as f:
    writer = csv.writer(f, lineterminator='\n')
    for row in image_csv:
        writer.writerow(row)

取得した
問題、答え、挿図, 選択肢 を整形して 4つのcsv ファイルを作成しました

image.png

次回

インストール他 準備編

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?