如何使用 Python BeautifulSoup 抓取 ID
How to scrape ID using Python BeautifulSoup
我想使用 Python 中的 BeautifulSoup 来抓取 div class = 大小以及 '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']
你走在正确的轨道上。
要获取标签的属性,请使用 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
我想使用 Python 中的 BeautifulSoup 来抓取 div class = 大小以及 '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']
你走在正确的轨道上。
要获取标签的属性,请使用 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