如何使用Python BeautifulSoup刮取ID

时间:2018-03-07 09:11:30

标签: python beautifulsoup screen-scraping

我想把div class = size以及' ID'在Python中使用BeautifulSoup的值。

<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']

1 个答案:

答案 0 :(得分: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