Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Why beautiful soup select by class return empty list by find_all works?

I am trying to parse out the top 100 hot hits for a certain day from billboards with beautiful soup. I tried to select the section by class name, but it does not work. I tried to use find_all by class, and it works. Why does find_all only work in this case?

from bs4 import BeautifulSoup
import requests

billboard_website = "https://www.billboard.com/charts/hot-100/2019-05-09"
response = requests.get(f"{billboard_website}")
soup = BeautifulSoup(response.text, "html.parser")


print(soup.select(".chart-element__information__song text--truncate color--primary")) # returns an empty list

print(soup.find_all(class_="chart-element__information__song text--truncate color--primary")) # returns the full list
question from:https://stackoverflow.com/questions/65545425/why-beautiful-soup-select-by-class-return-empty-list-by-find-all-works

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

1 Answer

0 votes
by (71.8m points)

Both work depending on what you are trying to achieve, as explained in the docs

Essentially find_all(class_="Class1 Class2") is almost equivalent to select(.Class1.Class2) the only difference is that the former requires the exact string while the latter requires CSS selectors but does not care about the order[1], e.g.

Say you have the following element <p class="A B">

find_all(class_="B A") won't match but select(.B.A) will

If you fix your syntax to soup.select(".chart-element__information__song.text--truncate.color--primary") it should return the same values

[1] - There are other differences such as support for regex, which are explained in the docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...