Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

web scraping - Python BeautifulSoup findAll by "class" attribute

I want to do the following code, which is what BS documentation says to do, the only problem is that the word "class" isn't just a word. It can be found inside HTML, but it's also a python keyword which causes this code to throw an error.

So how do I do the following?

soup.findAll('ul', class="score")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem seems to be that you expect find_all in the soup to find an exact match for your string. In fact:

When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes:

You can properly search for a class tag as @alKid said. You can also search with the class_ keyword arg.

soup.find_all('ul', class_="score")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...