Skip to main content
 首页 » 编程设计

Python BeautifulSoup 检查输出

2024年10月17日8EasonJim

我尝试检查网站打印的内容。 想象一下:

page = requests.get("testsite.com") 
soup = BeautifulSoup(page.content) 
if soup in ["my", "wished", "output"]: 
    print "It's there!" 
else: 
    print "It's not there!" 

所以我想检查网站是否包含文本“my”、“wished”或“output”。我该怎么做?

谢谢!

请您参考如下方法:

page = requests.get("testsite.com") 
test = ["my", "wished", "output"] 
if any(t in page.text for t in test): 
    print("It's there!") 
else: 
    print("It's not there!") 

soup 是 BeautifulSoup 对象,如果你想检查字符串中是否有文本,你应该使用 string object 比如 page.text

any(iterable) Return True if any element of the iterable is true. If the iterable is empty, return False.