我将分析许多具有不同 html 的网站,并尝试使用 BeautifulSoup 查找包含特定文本(在 html 内)的所有行。
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")
for text in soup.find_all():
if "price" in text:
print text
这种方法不起作用(尽管“价格”在 html 中被提及超过 40 次)。也许有更好的方法来做到这一点?
最佳答案
为什么不让 BeautifulSoup
找到包含所需文本的节点:
for node in soup.find_all(text=lambda x: x and "price" in x):
print(node)
关于python - BeautifulSoup 查找所有出现的特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176278/