0

我将如何从这里提取“我们很抱歉”文本

<div class="c-capr-add__input-na">
                We're sorry. This product is not available online at this time.
            </div>

并把它像

return out_of_stock_divs.text == "We're sorry. This product is not available online at this time."

如果您需要更多信息,请告诉我。非常感谢您!

4

1 回答 1

1

我假设你只有 html,所以我将从那部分开始:

html = """<div class="c-capr-add__input-na">
                We're sorry. This product is not available online at this time.
            </div>"""

soup = BeautifulSoup(html,'html.parser')

在这里,我们找到具有所需类的div 。这将返回与选项匹配的元素列表,在这种情况下,我们只有一个,因此我们选择第 0 个索引:

div = soup.find_all('div', {'class':'c-capr-add__input-na'})[0] 

现在我们只需要获取 text 和 stripit 因为它可以有前导/尾随空格和换行符:

txt = div.getText()
clean_txt = txt.strip()

您可以随心所欲地使用clean_txt.

于 2021-01-16T22:33:28.033 回答