In the Google Chrome's Inspect element tool you can: right-click on an element > copy > copy js path
and you get a nice snippet of code like the following: document.querySelector("#left-container > div.left-content > div > div > ul")
that easily gives you the "path" to a selected element in Javascript.
My question is if there is an easy way to turn this javascript snippet into Python possibly using BeautifulSoup that can give me the elements I want from a webpage.
In the Google Chrome's Inspect element tool you can: right-click on an element > copy > copy js path and you get a nice snippet of code like the following: document. querySelector("#left-container > div. left-content > div > div > ul") that easily gives you the "path" to a selected element in Javascript.
The other alternative is element. query / queryAll . These are alternative methods to querySelector and querySelectorAll that exist on DOM parent nodes. They also take selectors, except these selectors are interpreted relative to the element being queried from.
getElementById matches the id attributes to find DOM nodes, while querySelector searches by selectors. So for an invalid selector e.g <div id="1"></div> , getElementById('1') would work while querySelector('#1') would fail, unless you tell it to match the id attribute (e.g querySelector('[id="1"]') .
querySelector() and querySelectorAll() are two jQuery functions which helps the HTML elements to be passed as a parameter by using CSS selectors ('id', 'class') can be selected.
BeautifulSoup has CSS selectors support - use select()
or select_one()
methods:
soup = BeautifulSoup(html, 'html.parser')
elements = soup.select("#left-container > div.left-content > div > div > ul")
Make sure to use version 4.7.0 or above to have support for the most CSS4 selectors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With