LoginSignup
1
0

More than 3 years have passed since last update.

scrapyのCSSセレクタ(cssselectパッケージ)で `:nth-of-type() is not implemented` に遭遇した場合

Posted at

概要

scrapyで特定のCSSセレクタを指定する際にサボって死んだ話。
対象は cssselect==1.0.3

結論

:nth-of-type()はちゃんとタグを指定しましょう。

事象

とあるWebサイトをクロールする際に

.article-list > .content-box:nth-of-type(1) > div > div

以下のようなCSSセレクタを記述したところ

cssselect.xpath.ExpressionError: *:nth-of-type() is not implemented

というエラーが発生した。

調査

Githubで該当のエラーメッセージを検索すると

    def xpath_nth_of_type_function(self, xpath, function):
        if xpath.element == '*':
            raise ExpressionError(
                "*:nth-of-type() is not implemented")
        return self.xpath_nth_child_function(xpath, function,
                                             add_name_test=False)

どうやらelementを指定していない場合以外は動く様子。

そもそも、この :nth-of-type() は同じタグの中での処理なので完全にここで書いてないのはサボりだ。

というわけで、ちゃんとタグを補ったら動いた。

あとがき

そもそも、順番を考慮してパースしないといけないHTMLを扱っている状況自体がつらい....

1
0
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
1
0