Search code examples
pythonhtmlbeautifulsoup

Python Equivalent to Javascript querySelector


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.


Solution

  • 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.