最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Find all "a" tags in multiple divs with same class with BeautifulSoup - Stack Overflow

matteradmin4PV0评论

I want to find all "a" elemnts in multiple divs with same class.

from bs4 import BeautifulSoup
links = soup.find_all("div", class_="va-columns").find_all("a")

but this doesnt work and gives me an error. Can someone help me? Im trying to find all links on the main content of a website.

I want to find all "a" elemnts in multiple divs with same class.

from bs4 import BeautifulSoup
links = soup.find_all("div", class_="va-columns").find_all("a")

but this doesnt work and gives me an error. Can someone help me? Im trying to find all links on the main content of a website.

Share Improve this question asked Nov 17, 2024 at 16:46 Origami MaxOrigami Max 33 bronze badges 2
  • What's the error? – interjay Commented Nov 17, 2024 at 16:48
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Nov 17, 2024 at 19:41
Add a comment  | 

2 Answers 2

Reset to default 1

soup.find_all("div") will return a list. So you could simply loop through that list and for each div, you do div.find_all('a'). This way, you could have a list of all a tags in all div tags you wanted to search for.

Here's the code.

from bs4 import BeautifulSoup
links = [div.find_all("a") for div in soup.find_all("div", class_="va-columns")]

See this if you didn't get the for loop inside the list. (it's called a list comprehension in Python)

find_all() returns a list, so you would have to loop over the elements, calling find() on each of them and collecting all the results.

Instead you can use soup.select(), which takes a CSS-style selector.

links = soup.select("div.va-columns a")
Post a comment

comment list (0)

  1. No comments so far