Bs4 返回"[]"如何修复此错误?



我试图使一个代码,以确定他们是否搜索使用已输入返回一个空白页或不,我这样做是通过识别是否有某些文本在屏幕上

import requests
from bs4 import BeautifulSoup
site = input("Input the domain of the top level domain: ")
textsearch = input("Input what text you're looking for: ")
r = requests.get(f"https://www.google.com/search?q=site%3A.{site}+%22{textsearch}%22")
soup = BeautifulSoup(r.text, 'html.parser')
search = soup.find_all(text="Try different keywords")
print(search)

问题是这段代码返回"[]"当我知道这个网页返回零结果,我如何去修复这个?

如果你需要做的只是看看这个短语是否出现在任何地方,那么你不需要bs4

just doif "Try different keywords" in r.text: print("ERROR no results")

最新更新