如何使用Python BeautifulSoup获取ID

2024-09-30 20:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python中使用BeautifulSoup来获取div class=size和'ID'值。在

<div class="size ">
 <a class="selectVar" id="23333" data="40593232" data-price="13000,00 €" data-tprice="" data-sh="107-42" data-size-original="92" data-eu="92" data-size-uk="5" data-size-us="5.5" data-size-cm="26.5" data-branch-2="1" data-branch-3="1" data-branch-4="1" data-branch-5="1" data-branch-6="1" data-branch-on="1">
  92
 </a>
</div>

我尝试了以下方法,但没有成功:

^{pr2}$

Tags: divbranchiddatasizeshpriceclass
1条回答
网友
1楼 · 发布于 2024-09-30 20:31:19

你在正确的轨道上。
{cd1>方法的属性

# Find the <div> tag 
product_div = soup.find('div', {'class': 'size '})

# Find the <a> tag within the div
product_tag = product_div.find('a')

# Get the 'id' attribute of the <a> tag
product_id = product_tag.attrs['id']

print(product_id) # 23333

相关问题 更多 >