使用BeautifulSoup4,在网站中查找每次以某个符号开头的文本



我正试图使用python从网站上为一件商品刮取价格。

import requests
from bs4 import BeautifulSoup
URL = "https://..."
result = requests.get(URL)
doc = BeautifulSoup(result.text, "html.parser")
prices = doc.find_all(???)
print(prices)

在问号中,我知道我可以写出要查找的完整字符串,但我希望它每次都能找到以"开头的文本$&";。

有可能吗?如果有,怎么做?

使用regular expression捕获以特定字符开头的标签,如下所示:

import re
from bs4 import BeautifulSoup
html = """
<p>$Show me</p>
<p>I am invisible</p>
<p>me too</p>
<p>$Show me too</p>
"""
soup = BeautifulSoup(html, 'html.parser')
result = soup.find_all("p", text=re.compile("^$"))
# -> [<p>$Show me</p>, <p>$Show me too</p>]

注意,我使用了在$之前操作的,因为美元符号本身是一个特殊字符。有关详细信息,请参阅正则表达式语法。

最新更新