如何使用Python BeautifulSoup刮取ID

问题描述 投票:0回答:1

我想使用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>

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

product = soup.find("div", {'class': 'size ', 'type':'id'})['value']
python beautifulsoup screen-scraping
1个回答
1
投票

你走在正确的轨道上。 要获取标记的属性,请使用tag.attrs方法:

# 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
© www.soinside.com 2019 - 2024. All rights reserved.