首页 > 解决方案 > BeautifulSoup: searching for multiple tags

问题描述

I'm currently implementing Beautiful Soup in my application to search through a user's HTML content. How it works is that if the user does not input one of the HTML tags specified, it returns a false value. This works fine when there is only one HTML tag, however I also want this to work for multiple HTML tags. Here is my code.

soup = BeautifulSoup(userInput, 'html.parser')
if soup.find(['h1','h2']) is None:
   return false
else:
   return true

So how I want it to work is that if all elements in the array [h1,h2] are not in the user input, then it will return false. How the code above is functioning is that if the user enters one of the values in the array it will return true.

Is there any way to implement this in Python?

标签: pythonbeautifulsoup

解决方案


.find不能这样工作。您必须为每个标签调用一次:

if not any(soup.find(tag) for tag in ('h1', 'h2')):
    # if inputted HTML does not contain h1 or h2 tags

您当然可以使用您需要的任何逻辑。如果要强制同时存在h1h2标签,请使用

if not all(soup.find(tag) for tag in ('h1', 'h2'))

推荐阅读