从汤中提取标签与美丽的小屋
'''
<div class="kt-post-card__body>
<div class="kt-post-card__title">Example_1</div>
<div class="kt-post-card__description">Example_2</div>
<div class="kt-post-card__bottom">
<span class="kt-post-card__bottom-description kt-text-truncate" title="Example_3">Example_4</span>
</div>
</div>
'''
根据我附加的图片,我想提取所有“ kt-post-card__body” attrs,然后从其中每个属性中提取:提取:
("kt-post-card__title", "kt-post-card__description")
像列表一样。
我尝试了一下:
ads = soup.find_all('div',{'class':'kt-post-card__body'})
但是使用ads [0] .div
我仅访问“ kt-post-card__title”
而“ kt-post-card-card__body” 还有其他三个子标签,例如:“ kt-post-card__description”
and “ kt-post-card __bottom”
...,为什么那是?
'''
<div class="kt-post-card__body>
<div class="kt-post-card__title">Example_1</div>
<div class="kt-post-card__description">Example_2</div>
<div class="kt-post-card__bottom">
<span class="kt-post-card__bottom-description kt-text-truncate" title="Example_3">Example_4</span>
</div>
</div>
'''
according to picture I attached, I want to extract all "kt-post-card__body" attrs and then from each one of them, extract:
("kt-post-card__title", "kt-post-card__description")
like a list.
I tried this:
ads = soup.find_all('div',{'class':'kt-post-card__body'})
but with ads[0].div
I only access to "kt-post-card__title"
while "kt-post-card__body"
has three other sub tags like: "kt-post-card__description"
and "kt-post-card__bottom"
... , why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您的问题还不清楚 - 提取类:
输出:
要获取文本,您还必须迭代
Resultset
,并且可以访问每个元素文本以填写您的列表或使用stripped_strings < /代码>。
示例
输出:
或
输出:
Cause your question is not that clear - To extract the classes:
Output:
To get the texts you also have to iterate your
ResultSet
and could access each elements text to fill your list or usestripped_strings
.Example
Output:
or
Output:
尝试以下操作:
我认为您只得到第一个Div,因为您称为
ads [0] .div
Try this:
I think you're getting only the first div because you called
ads[0].div