gpt4 book ai didi

python - 如何查找
的特定属性的值

转载 作者:行者123 更新时间:2023-12-01 01:45:38 24 4
gpt4 key购买 nike

嗯,首先我不确定它是否是一个属性。如果不是,请告诉我正确的名字。该页面有一个div标签,其一般格式为:

<div class="top" data-src="/a/path/to/another/page.html" id="id_random" style="position: absolute; left: 0px; top: 0px;">

我想使用 python/bs4 检索上面“data-src”属性(?)的值,即“/a/path/to/another/page.html”。

我已经将上面的 html 代码放在名为 target_div 的变量中,并且我尝试过以下操作:

target_div.find(id='data-src')

target_div.find ('meta', {'name':'data-src'}) 

target_div.find (attrs={'name' : 'data-src'})

请问如何在 div 中查找“data-src”及其值?

致以诚挚的问候,

最佳答案

你可以尝试这样:

from bs4 import BeautifulSoup

target_div = '''<div class="top" data-src="/a/path/to/another/page.html" id="id_random" style="position: absolute; left: 0px; top: 0px;">'''

soup = BeautifulSoup(target_div, 'html.parser')
print soup.find('div', class_='top')['data-src']

结果应该是:

/a/path/to/another/page.html

关于python - 如何查找 <div > 的特定属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51371150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com