简体   繁体   English

无法使用 python 中的 beautifulsoup 获取 div 内的所有 id 标签和 a/href 标签

[英]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 tagsid

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中的所有idlinks

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.

相关问题 使用 BeautifulSoup + Python 从列表中获取所有 href 标签和链接 - Get all href tags and links from a list using BeautifulSoup + Python 如何在Python中使用Beautifulsoup查找div中的所有锚标签 - How to find all anchor tags inside a div using Beautifulsoup in Python Python BeautifulSoup 未找到所有href标签 - Python BeautifulSoup not finding all href tags 尝试使用 beautifulsoup 获取 ul 中的所有 li 标签 - Trying to get all li tags inside ul using beautifulsoup Python:无法<span>使用 BeautifulSoup</span>获取所有<span>标签</span>中的所有文本 - Python: failed to get all the text in all the <span> tags using BeautifulSoup 在BeautifulSoup中正确获取href标签 - Get href tags properly in BeautifulSoup 如何使用Python中的BeautifulSoup获取XML文件中所有具有相同名称的标签? - How to get all the tags with same name inside an XML file using BeautifulSoup in Python? 如何从<a href>标签中</a>获取信息<div> <a href>BeautifulSoup 和 Python 的标签?</a> - How can I get information from an <a href> tag within <div> tags with BeautifulSoup and Python? 无法使用Python Beautifulsoup获取所有标记/文本抓取网站 - Not able to get all tags/text scraping a website using Python Beautifulsoup 通过 python 中的 BeautifulSoup 获取使用特定样式的所有标签 - get all tags using specific style by BeautifulSoup in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM