从汤中提取标签与美丽的小屋

发布于 2025-01-30 13:02:22 字数 1021 浏览 3 评论 0原文

'''
<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” ...,为什么那是?

enter image description here

'''
<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2025-02-06 13:02:23

因为您的问题还不清楚 - 提取类:

for e in soup.select('.kt-post-card__body'):
    print([c for t in e.find_all() for c in t.get('class')])

输出:

['kt-post-card__title', 'kt-post-card__description', 'kt-post-card__bottom', 'kt-post-card__bottom-description', 'kt-text-truncate']

要获取文本,您还必须迭代Resultset,并且可以访问每个元素文本以填写您的列表或使用stripped_strings < /代码>。

示例
from bs4 import BeautifulSoup

html_doc='''
<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>
'''

soup = BeautifulSoup(html_doc)

for e in soup.select('.kt-post-card__body'):
    data = [
        e.select_one('.kt-post-card__title').text,
        e.select_one('.kt-post-card__description').text      
    ]
    print(data)        

输出:

['Example_1', 'Example_2']

print(list(e.stripped_strings))

输出:

['Example_1', 'Example_2', 'Example_4']

Cause your question is not that clear - To extract the classes:

for e in soup.select('.kt-post-card__body'):
    print([c for t in e.find_all() for c in t.get('class')])

Output:

['kt-post-card__title', 'kt-post-card__description', 'kt-post-card__bottom', 'kt-post-card__bottom-description', 'kt-text-truncate']

To get the texts you also have to iterate your ResultSet and could access each elements text to fill your list or use stripped_strings.

Example
from bs4 import BeautifulSoup

html_doc='''
<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>
'''

soup = BeautifulSoup(html_doc)

for e in soup.select('.kt-post-card__body'):
    data = [
        e.select_one('.kt-post-card__title').text,
        e.select_one('.kt-post-card__description').text      
    ]
    print(data)        

Output:

['Example_1', 'Example_2']

or

print(list(e.stripped_strings))

Output:

['Example_1', 'Example_2', 'Example_4']
夜清冷一曲。 2025-02-06 13:02:23

尝试以下操作:

ads = soup.find_all('div',{'class':'kt-post-card__body'})

ads[0]

我认为您只得到第一个Div,因为您称为ads [0] .div

Try this:

ads = soup.find_all('div',{'class':'kt-post-card__body'})

ads[0]

I think you're getting only the first div because you called ads[0].div

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文