Beautiful Soup cannot find a CSS class if the object has other classes, too

后端 未结 4 1754
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 18:13

if a page has

and

, then soup.findAll(True, \'class1\') will find them both.

4条回答
  •  温柔的废话
    2020-11-27 18:21

    It’s very useful to search for a tag that has a certain CSS class, but the name of the CSS attribute, “class”, is a reserved word in Python. Using class as a keyword argument will give you a syntax error. As of Beautiful Soup 4.1.2, you can search by CSS class using the keyword argument class_:

    Like:

    soup.find_all("a", class_="class1")
    

提交回复
热议问题