Using python BeautifulSoup, how to find all 'a' tags with NOT class

Viewed 390

Let's say I have 4 links:

<a href="#1" id="xyz" class="monte">hi</a>
<a href="#3" id="qrs" class="sam">hi</a>
<a href="#6" id="mno" class="alex">hi</a>
<a href="#9" id="abc" >hi</a>

I want to return the href, class and id for all elements that do NOT have the class = "monte" ... including the one element without a class element at all. Let's assume the above is called html

Is there a NOT operator such as !

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")  # or lxml 
result = soup.find_all('a', {"class": !"monte"})
for link in result:
    print(link.get("href"));
    print(link.get("class"));
    print(link.get("id"));

Using selenium driver, I would like to click on any of the found elements. Assume that I may not have a unique id to identify what to click on. That is, I may need to use find_element_by_xpath. I do have a unique data-id.

3 Answers

Use :not to exclude a particular class(es). Use .has_attr to test if class present when attempting to access or use .get with a default value e.g. print(i.get('class', 'None')).

from bs4 import BeautifulSoup as bs
from pprint import pprint

html = '''
<a href="#1" id="xyz" class="monte">hi</a>
<a href="#3" id="qrs" class="sam">hi</a>
<a href="#6" id="mno" class="alex">hi</a>
<a href="#9" id="abc" >hi</a>
'''

soup = bs(html, 'lxml')

for i in soup.select('a:not(.month)'):
    print(i['href'])
    print(i['id'])
    if i.has_attr('class'):
        print(i['class'])

If you want to navigate to them then you will need to use .get method on the hrefs:

links = [i['href'] in soup.select('a:not(.month)')]

for link in links:
    driver.get(link)

Please try this:

soup.find_all('a', class_=lambda x: x != 'monte')

Skip the one you dont want in iteration.

soup = BeautifulSoup(html, "html.parser")  # or lxml 
result = soup.find_all('a')
for link in result:
    if link.get("class")==None:
        print(link.get("href"))
        print(link.get("id"))
        print(link.get_text())
    elif 'monte' not in link.get("class") :
        print(link.get("href"))
        print(link.get("class"))
        print(link.get("id"))
        print(link.get_text())
Related