AWS / PHP / Python ちょいメモ

amazon web service , PHP, Python を使ったときのメモ。日本語でググってもわからなかった事を中心に。

Beautiful Soup 4.x では parser を明示指定しよう

pythonスクレイピングなどを行うときに便利なのが BeautifulSoup (ここでは bs4 を扱っています) 。
parserを選択できる仕様になっていますが、 4.3.xまでは明示的に指定しなくても、適度に動いていました (どう動いてたかは、調べてない)。

こちらにあるように、lxmlやhtml5libがインストールされている場合はそちらが優先されるそうです。


4.4.x からは明示的に指定しないと、自動選択されるとともにWARNING がでて、何を使って動いてのかを教えてくれます。

lxml が自動で選ばれた例)

soup = BeautifulSoup(html)
..python2.7/site-packages/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

続きを読む