
前回はドライバー名が格納できたので、今回はそれを以外のデータを取り出していきたいと思います。
以前、[F1公式サイトのスクレイピング](アドレス)は以下から。
以前、スクレイピングの際に調べたやつ
以前、スクレイピングで調べた際の情報です。
- 【ドライバー名】
- ファーストネーム→クラス名(hide-for-tablet)、タグ(span)
 - ファミリーネーム→クラス名(hide-for-mobile)、タグ(span)
 
 - 【国籍】
- クラス名(dark semi-bold uppercase)、タグ(td)
 
 - 【所属チーム】
- クラス名(grey semi-bold uppercase ArchiveLink)、タグ(a)
 
 - 【獲得ポイント】
- クラス名(dark bold)、タグ(td)
 
 
という感じになります。
国籍と所属チームを取り出す
それぞれのクラス名は
- dark semi-bold uppercase
 - grey semi-bold uppercase ArchiveLink)
 
となっています。これを前回までのスクリプトに加えてみましょう。if~instrを使ってrepeat~loopの中に組み込んでいきます。まずは国籍ですが、最初にどんな1行になっているのかを確認するために以下のコードを入れ込んで実行してみます。
if (instr(text_line, 0, "dark semi-bold uppercase") ! -1) {
    dialog text_line
    continue
}
結果は

国籍の場合は今までのspanではなく、tdです(上のスクレイピング時の情報にも書いてあるんですがね)。注意してください。具体的には以下のようになります。
これを踏まえて次のようなスクリプトになりました。
if (instr(text_line, 0, "dark semi-bold uppercase") ! -1) {
    strrep text_line, "<td class=\"dark semi-bold uppercase\">", ""
    strrep text_line, "</td>", ""
    strrep text_line, " ", ""
    country(country_cnt) = text_line
    country_cnt++
    continue
}
一方、所属チームの方は取り除く方式ではちょっとまずくて


のようにurlがそれぞれ別々になっているため、同じ要素を単純に取り除くわけにはいかない状況。そこで前回書いていたもうひとつの方法を用いて取り出していきます。
if (instr(text_line, 0, "grey semi-bold uppercase ArchiveLink") ! -1) {
    split text_line, ">", buf
    split buf(1), "<", result
    teamname(teamname_cnt) = result(0)
    teamname_cnt++
    continue
}
これで国籍と所属チーム名が取り出せたはずですので、chkで表示させていきましょう。以下のように改造しました。
chk = ""
repeat 40
    if firstname(cnt) = "" : break
    chk += firstname(cnt) + " " + familyname(cnt) + "\t(" + country(cnt) + ") \t【" + teamname(cnt) + "】\n"
loop
mesbox chk, 640, 400
実行結果は

ばっちり取得できてますね。
獲得ポイントの取得
次は獲得ポイントです。これはスクレイピングの時にもひっかかったところなのですが、クラス名「dark bold」は2種類のタグで使われていて、そのうちのtdタグの方がポイントのデータが書かれている1行だったりします。そこで判定方法にひと工夫加えて
if (instr(text_line, 0, "dark bold") ! -1) and (instr(text_line, 0, "</td>") ! -1) {
    dialog text_line
    continue
}
「dark bold」があって、かつ「</td>」がある行のみを取り出しています。

無事取り出せました。これもポイントだけを取り出すように処理しちゃいましょう。上記の確認用のスクリプトを以下のように書き換えます。
if (instr(text_line, 0, "dark bold") ! -1) and (instr(text_line, 0, "</td>") ! -1) {
    strrep text_line, "<td class=\"dark bold\">", ""
    strrep text_line, "</td>", ""
    strrep text_line, " ", ""
    getpoint(point_cnt) = text_line
    point_cnt++
    continue
}
さらに、確認部分も
chk += firstname(cnt) + " " + familyname(cnt) + "\t(" + country(cnt) + ") \t【" + teamname(cnt) + "】 " + getpoint(cnt) + "p\n"
と書き換えておきます。

ポイントも無事に取り出せました。
まとめ
スクレイピングでやってたことをHSPでも無事に実行できました。次はいよいよページの作成にとりかかりたいと思います。
今回の全コードは以下のようになりました。試してみてくださいね。(その1でダウンロードしたdrivers.htmlが必要です)
//HSPモジュール SAKMISさんのを使用しています
//命令→lfcc ファイルネーム
//読み込み→改行置換→保存
    #module
    #deffunc lfcc str filename
;   mref filename,32
;   mref status,64
        exist filename
        size=strsize
        if size=-1 : status=-1 : return
        sdim ss,size+1,1
        bload filename,ss,size
        ii=0
        code=0
        sdim data,size<<1,1
    repeat size
        tt = peek (ss,cnt)
        if tt=10 : code=10 : break
        if tt=13 {
        code=13
        tt = peek (ss,cnt+1)
        if tt=10 : code=0
        break
        }
    loop
        if code=0 : status=-1 : return
    repeat size
        tt = peek (ss,cnt)
        if tt=code : wpoke data,ii,2573 : ii+2 : continue
        poke data,ii,tt : ii++
    loop
        bsave filename,data,ii
        status=ii
    return
#global
#include "hspinet.as"
// ネット接続の確認
netinit
if stat : dialog "ネット接続できません" : end
// 初期設定
download_url = "https://www.formula1.com/en/results.html/2016/drivers.html"
sdim firstname,  40, 40
sdim familyname, 40, 40
sdim country,    40, 40
sdim teamname,   40, 40
sdim getpoint,   40, 40
lfcc "drivers.html"
notesel htmlfile
noteload "drivers.html"
first_cnt = 0 : family_cnt = 0 : country_cnt = 0 : teamname_cnt = 0
repeat notemax
    noteget text_line, cnt
    // ファーストネーム
    if (instr(text_line, 0, "hide-for-tablet") ! -1) {
        strrep text_line, "<span class=\"hide-for-tablet\">", ""
        strrep text_line, "</span>", ""
        strrep text_line, " ", ""
        firstname(first_cnt) = text_line
        first_cnt++
        continue
    }
    // ファミリーネーム
    if (instr(text_line, 0, "hide-for-mobile") ! -1) {
        strrep text_line, "<span class=\"hide-for-mobile\">", ""
        strrep text_line, "</span>", ""
        strrep text_line, " ", ""
        familyname(family_cnt) = text_line
        family_cnt++
        continue
    }
    // 国籍
    if (instr(text_line, 0, "dark semi-bold uppercase") ! -1) {
        strrep text_line, "<td class=\"dark semi-bold uppercase\">", ""
        strrep text_line, "</td>", ""
        strrep text_line, " ", ""
        country(country_cnt) = text_line
        country_cnt++
        continue
    }
    // 所属チーム
    if (instr(text_line, 0, "grey semi-bold uppercase ArchiveLink") ! -1) {
        split text_line, ">", buf
        split buf(1), "<", result
        teamname(teamname_cnt) = result(0)
        teamname_cnt++
        continue
    }
    // 獲得ポイント
    if (instr(text_line, 0, "dark bold") ! -1) and (instr(text_line, 0, "</td>") ! -1) {
        strrep text_line, "<td class=\"dark bold\">", ""
        strrep text_line, "</td>", ""
        strrep text_line, " ", ""
        getpoint(point_cnt) = text_line
        point_cnt++
        continue
    }
loop
noteunsel
//debug
chk = ""
repeat 40
    if firstname(cnt) = "" : break
    chk += firstname(cnt) + " " + familyname(cnt) + "\t(" + country(cnt) + ") \t【" + teamname(cnt) + "】 " + getpoint(cnt) + "p\n"
loop
mesbox chk, 640, 400
stop
そこそこ長くなってきましたね。