美汤发现问题



我试图用Python解析一个网页->Beautiful Soup和文本加载后约2秒只出现:[1]这是关于HTML的图片关于提取

我正在尝试提取突出显示的(Sold Out)的内容。

price = soup.find('button', attrs={'class':'ncss-btn-primary-dark'})

但是这里有一个错误代码

price = soup.find('button', attrs={'class':'ncss-btn-primary-dark  btn-lg btn-lg disabled'}).text AttributeError: 'NoneType' object has no attribute 'text'

如何从上面显示的图像中获得文本Sold Out ?或者我是否需要一些东西来等待文本显示,只有能够找到它?

不错的尝试,把。text改成#。文本和提到的attrs={'class': 'ncss-btn-primary-dark'}.

如果有不理解的地方,请查看下面的代码,特别是属性。

遵循下面提到的代码,它将工作。

from bs4 import BeautifulSoup
s = """
<button type="button" class="ncss-btn-primary-dark  btn-lg btn-lg disabled">Sold Out</button>
"""
soup = BeautifulSoup(s, 'html.parser')
#print(soup.find('button', {'class': 'whatever class name mentioned on your code type here and try'}).text)
print(soup.find('button', {'type': 'button'}).text)
#(or) this below mentioned attribute method also working
print(soup.find('button', attrs={'type': 'button'}).text)

最新更新