[英]Unable to get all the id tags and a/href tags inside a div using beautifulsoup in python
This is the html code:这是 html 代码:
<div class="main_class">
<a id = "link_id" href = "link1"></a>
<a id = "link_id" href = "link2"></a>
<a id = "link_id" href = "link3"></a>
<a id = "link_id" href = "link4"></a>
</div>
All the id
's have the same name所有的
id
都有相同的名字
I tried using beautiful soup to extract all the a tags
and id
under a specific div
我尝试使用漂亮的汤来提取特定
div
下的所有a tags
和id
These are the methods I used:这些是我使用的方法:
filtered = soup.find_all("div[class=main_class")
filtered = soup.find_all("div", {"id": "link_id"})
filtered = soup.find_all('id', href = True) # Returns extra links which are not in the specific div
I have tried some other methods but I am unable to get all the id
and links
inside a specific div
我尝试了其他一些方法,但我无法获取特定
div
中的所有id
和links
Try this:尝试这个:
links = soup.find("div", {"class": "main_class"}).findChildren("a", {"id": "link_id"})
>>> links
[<a href="link1" id="link_id"></a>,
<a href="link2" id="link_id"></a>,
<a href="link3" id="link_id"></a>,
<a href="link4" id="link_id"></a>]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.