【问题标题】:Beautiful Soup find issue美丽的汤发现问题
【发布时间】:2023-03-27 00:14:01
【问题描述】:

我正在尝试使用 Python->Beautiful Soup 解析网页,加载大约 2 秒后的文本仅出现: [1]Here is the Image about the HTML about the extract

我正在尝试提取突出显示(已售罄)的内容。

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?还是我需要一些东西来等待文本显示才能找到它?

【问题讨论】:

标签: python beautifulsoup find extract error-code


【解决方案1】:

不错的尝试,将 .text 更改为 #.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)

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 2013-10-30
    • 2016-05-19
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多