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
2 Answers
Reset to default 1soup.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条)