[Python] Beautiful Soup 4 Synonyms with JavaScript

Synonyms - Python Beautiful Soup 4 translated to JavaScript.

Python Beautiful Soup 4 vs JavaScript
Python Beautiful Soup 4 JavaScript

bs4 making the soup (document object)

from bs4 import BeautifulSoup

with open(path, 'r') as f:
    document = BeautifulSoup(f)
document object is built-in

getElementById (bs4 find)

foo = document.find(id="foo")


var foo = document.getElementById("foo");

getElementsByTagName (bs4 find_all)

divs = document.find_all("div")
# or
divs = element.find_all("div")


var divs = document.getElementsByTagName("div");
// or
var divs = element.getElementsByTagName("div");

Searching by CSS class

# find first one div with class name page
div = document.find("div", class_="page")
# find all divs with class name page
divs = document.find_all("div", class_="page")
# or
div = element.find("div", class_="page")
divs = element.find_all("div", class_="page")

querySelector and querySelectorAll

// find first one div with class name page
var div = document.querySelector("div.page");
// find all divs with class name page
var divs = document.querySelectorAll("div");
// or
var div = element.querySelector("div.page");
var divs = element.querySelectorAll("div");


href = element.get("href")


var href = element.getAttribute("href");

Tested on: Ubuntu Linux 15.10, Python 2.7.10.


[1][Python] Export PIXNET Blog to reStructuredText Files