我试图从以下
<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/