Beautiful Soup Tricks

Sanjiv Gautam
1 min readFeb 2, 2021

--

FREQUENTLY OCCURING PROBLEM:

https://stackoverflow.com/questions/7591535/beautifulsoup-attributeerror-navigablestring-object-has-no-attribute-name

USING QUERY SELECTORS

thanks to (https://stackoverflow.com/questions/24801548/how-to-use-css-selectors-to-retrieve-specific-links-lying-in-some-class-using-be)

soup.select('div')
# All elements named <div>

soup.select('#author')
# The element with an id attribute of author

soup.select('.notice')
# All elements that use a CSS class attribute named notice

soup.select('div span')
# All elements named <span> that are within an element named <div>

soup.select('div > span')
# All elements named <span> that are directly within an element named <div>,
# with no other element in between

soup.select('input[name]')
# All elements named <input> that have a name attribute with any value

soup.select('input[type="button"]')
# All elements named <input> that have an attribute named type with value button

MORE QUERIES:

  1. This selects all the divs which have EITHER A or B
soup.findAll('div', class_=['A', 'B'])

2 . This would work if the class only had A and B. For instance <div class="A B C" /> would not be caught.

soup.findAll('div', class_=['A B'])

--

--

Sanjiv Gautam
Sanjiv Gautam

Written by Sanjiv Gautam

Just an average boy who wishes mediocrity over luxury.

No responses yet