admin管理员组文章数量:1434890
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")
本文标签: pythonFind all quotaquot tags in multiple divs with same class with BeautifulSoupStack Overflow
版权声明:本文标题:python - Find all "a" tags in multiple divs with same class with BeautifulSoup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745631330a2667291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论