よしたく blog

ほぼ週刊で記事を書いています

Beautiful Soupでhtmlファイルを開こうとするときに出るエラー

目次

  1. はじめに
  2. エラー解決

はじめに

Beautiful Soupを使うときはwebのページをスクレイピングするときが多いと思うが、htmlファイルを開きたいときにエラーが発生したのでメモ。

エラー解決

Beautiful Soupを使って、htmlファイルを開こうとする。

hoge_html = "hoge.html"
bsObj = BeautifulSoup(hoge_html, "html.parser")

こんなエラーが出る。 "hoge.html" looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.

エラーにも書いてあるが、これはファイルのパスを指定してBeautiful Soupに投げているだけなので、

filename = "hoge.html"
hoge_html = open(filename,'r')
bsObj = BeautifulSoup(hoge_html, "html.parser")

ファイルをオープンして再度投げればうまくいきます