简体   繁体   中英

How to use re.compile with class in BeautifulSoup

I am trying to scrape a page with different css classes as follows:

class="mod result idx0 people"
class="mod result idx1 people"
class="mod result idx2 people"
class="mod result idx3 people"
class="mod result idx4 people"

I am using bs4 and html5lib with Python2.7

Now when I do this for first element above I get the results positively.

soup.find(class_="mod result idx0 people")

However, I want to do this for all the classes.

So I am trying all of them but am still getting an empty list [] . What am I doing wrong?

soup.find_all(class_="mod result")
soup.find_all(class_=re.compile("mod result"))
soup.find_all("li",{"class":re.compile("mod result")})
soup.find_all("li",attrs={"class":re.compile("mod result")})
soup.find_all({"class":re.compile("mod result")})

None of them are working :-(

根据( Beautiful Soup文档-按CSS类搜索 ),如果要搜索与两个或多个CSS类匹配的标签,则应使用CSS选择器。

soup.select('.mod.result')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM