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

I want to find all "a" elemnts in multiple divs with same class.from bs4 import BeautifulSou

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")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745631330a4637126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信