当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Multiple selection Beautiful Soup

问题描述:

I have a class named summary and it contains a lot of differents tags like 'p', 'h2', 'img', 'li' etc...

What I want to do is a search using that criteria. I've tried using select and find_all with no luck.

Select: data = soup.select('summary p') but I cant include more than one tag at once eg: h2

Find_all: data = soup.find_all(['p', 'h2']) here I can pass a list and it finds all tags, but I don't know how to narrow the search to the summary class

How can I do it?

Thank you in advance!

网友答案:

Your approach with find_all() is correct. You just have to pass the class as an attribute to the find_all(). Like this:

data = soup.find_all(['p', 'h2'], attrs={'class':'summary'})

It is documented here

分享给朋友:
您可能感兴趣的文章:
随机阅读: