如何使用Python BeautifulSoup抓取ID [英] How to scrape ID using Python BeautifulSoup

查看:69
本文介绍了如何使用Python BeautifulSoup抓取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python中的BeautifulSoup刮除div class = size和'ID'值.

I would like to scrape the div class = size along with 'ID' value using BeautifulSoup in Python.

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

我尝试了以下失败的尝试:

I Have tried the following with no success:

product = soup.find("div", {'class': 'size ', 'type':'id'})['value']

推荐答案

您处在正确的轨道上.
要获取标签的属性,请使用 tag.attrs 方法:

You're on the right track.
To get the attributes of a tag, use the tag.attrs method:

# 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抓取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆