How to Beautiful Soup (bs4) match just one, and only one, css class

后端 未结 7 629
说谎
说谎 2020-12-10 20:28

I am using following code to match all div that have CSS class \"ad_item\".

soup.find_all(\'div\',class_=\"ad_item\")

problem that I have i

7条回答
  •  一生所求
    2020-12-10 20:47

    I have found one solution, although it have nothing to do with BS4, it is pure python code.

    for item in soup.find_all('div',class_="ad_item"):
         if len(item["class"]) != 1:
             continue;
    

    It basically skip item, if there is more than one CSS class.

提交回复
热议问题