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

julia でスクレイピング

Posted at

以下のような感じでスクレイピングできます.
存在しないURLを使っているので,適宜URLを変更しましょう.

using HTTP, Gumbo, Cascadia

url = "https://hoge.example"

req = HTTP.request("GET", url)
doc = parsehtml(String(req.body))

hogelist = eachmatch(Selector(".Hoge"), doc.root)

HTTP.jl で web ページの情報を取得して, Gumbo.jl の parsehtml() で HTML をパースします.
Cascadia.jl を使えば,指定したセレクタにマッチする HTMLNode を取得できます.例えば以下のような感じです.HTMLNodetext() を使って文字列に変換できます.

julia> doc = parsehtml("<p id=\"hoge\"> Hello, world! </p>")
HTML Document:
<!DOCTYPE >
HTMLElement{:HTML}:<HTML>
  <head></head>
  <body>
    <p id="hoge">
      Hello, world!
    </p>
  </body>
</HTML>

julia> eachmatch(Selector("#hoge"), doc.root)
1-element Vector{HTMLNode}:
 HTMLElement{:p}:<p id="hoge">
  Hello, world!
</p>



julia> eachmatch(Selector("#hoge"), doc.root)[1] |> text
"Hello, world!"
0
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
0
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?