python - 无法使用 BeautifulSoup 获取 span 属性的文本

标签 python beautifulsoup

我正在尝试从以下内容中获取

<span id="SkuNumber" itemprop="identifier" content="sku:473768" data-nodeid="176579" class="product-code col-lg-4 col-md-4">ΚΩΔ. 473768</span></div>

data-nodeid 的值 我做了以下事情

price_nodes = soup.find('span', attrs={'id': 'SkuNumber'})
datanode = price_nodes.select_one('span[data-nodeid]')

但我得到“无” 我怎样才能解决这个问题?谢谢

最佳答案

如果price_nodes填写正确

price_nodes =

<span id="SkuNumber" itemprop="identifier" content="sku:473768" data-nodeid="176579" class="product-code col-lg-4 col-md-4">ΚΩΔ. 473768</span>

你只需要这样做:

datanode = price_nodes.get('data-nodeid')

完整代码应为:

from bs4 import BeautifulSoup as soup

html = '<div><span id="SkuNumber" itemprop="identifier" content="sku:473768" data-nodeid="176579" class="product-code col-lg-4 col-md-4">ΚΩΔ. 473768</span></div>'
page = soup(html, 'html.parser')
price_nodes = page.find('span', {'id': 'SkuNumber'})
datanode = price_nodes.get('data-nodeid')

关于python - 无法使用 BeautifulSoup 获取 span 属性的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60114339/

相关文章:

python - 我尝试在 Kickstarter 上获取不同项目的支持者和所在城市

python - 使用 bs4 查找特定链接文本

python-3.x - 如何从 target.com 产品页面抓取产品价格?

javascript - 使用 python 抓取从网页中嵌入的传单应用程序获取坐标

Python从子集中查找对(对数正在变化)

python - Django - 更改选择项目的显示

python - 将字符串转换为整数

python - 使用 BeautifulSoup 从标签中获取值(value)

Python Tweepy 280 个字符状态

python - Pandas ExcelWriter .save() 出错;权限错误 [WinError 32]