概要
paiza.ioでelixirやってみた。
練習問題やってみた。
練習問題
htmlをスクレイピングせよ。
参考にしたページ
サンプルコード
:inets.start
:ssl.start
url = "https://qiita.com/advent-calendar/2022/elixir"
url = String.to_charlist(url)
{:ok, {_status, _headers, body}} = :httpc.request(url)
body = IO.iodata_to_binary(body)
|> IO.iodata_to_binary
|> String.split("encryptedId")
|> Enum.map(fn s ->
Regex.named_captures(~r/"user":{"urlName":"(?<user>.*?)",/, s)
end)
|> Enum.slice(2..-1//1)
|> Enum.map(fn %{"user" => user} ->
user
end)
|> Enum.frequencies
|> Enum.sort_by(fn {_user, cnt} ->
cnt
end, :desc)
|> IO.inspect
|> Enum.reduce(0, fn {_user, cnt}, sum ->
sum + cnt
end)
|> IO.puts
実行結果
[
{"torifukukaiou", 42},
{"RyoWakabayashi", 41},
{"zacky1972", 25},
{"mnishiguchi", 24},
{"piacerex", 11},
{"the_haigo", 10},
{"ShigeItoEx", 7},
{"GeekMasahiro", 7},
{"Yoosuke", 7},
{"nako_sleep_9h", 5},
{"miwacchi", 5},
{"t-yamanashi", 5},
{"westbaystars", 3},
{"k-j-y", 3},
{"koga1020", 3},
{"ta_to_jp", 2},
{"kyawaguchi", 1},
{"mokichi", 1},
{"Gsann", 1},
{"a_utsuki", 1},
{"def_elixir", 1},
{"kikuyuta", 1},
{"ohr486", 1},
{"hisaway", 1},
{"nanbut14", 1},
{"Goody27", 1},
{"Alicesky2127", 1},
{"pojiro", 1},
{"tomoaki-kimura", 1},
{"koyo-miyamura", 1},
{"GKBR", 1},
{"MzRyuKa", 1},
{"tamanugi", 1},
{"myasu", 1},
{"tuchiro", 1}
]
219
成果物
以上。