beautifulsoupvspyquery
beautifulsoup is a Python library for pulling data out of HTML and XML files. It creates parse trees from the source code that can be used to extract data from HTML, which is useful for web scraping. With beautifulsoup, you can search, navigate, and modify the parse tree. It sits atop popular Python parsers like lxml and html5lib, allowing users to try out different parsing strategies or trade speed for flexibility.
beautifulsoup has a number of useful methods and attributes that can be used to extract and manipulate data from an HTML or XML document. Some of the key features include:
- Searching the parse tree
You can search the parse tree using the various search methods that beautifulsoup provides, such as find(), find_all(), and select(). These methods take various arguments to search for specific tags, attributes, and text, and return a list of matching elements. - Navigating the parse tree
You can navigate the parse tree using the various navigation methods that beautifulsoup provides, such as next_sibling, previous_sibling, next_element, previous_element, parent, and children. These methods allow you to move up, down, and around the parse tree. - Modifying the parse tree
You can modify the parse tree using the various modification methods that beautifulsoup provides, such as append(), extend(), insert(), insert_before(), and insert_after(). These methods allow you to add new elements to the parse tree, or to change the position of existing elements. - Accessing tag attributes
You can access the attributes of a tag using the attrs property. This property returns a dictionary of the tag's attributes and their values. - Accessing tag text
You can access the text within a tag using the string property. This property returns the text as a string, with any leading or trailing whitespace removed.
With the above feature one can easily extract data out of HTML or XML files. It is widely used in web scraping and other data extraction projects.
It also has features for parsing XML files, special methods for dealing with HTML forms, pretty printing HTML and a few other functionalities.
PyQuery is a Python library for working with XML and HTML documents. It is similar to BeautifulSoup and is often used as a drop-in replacement for it.
PyQuery is inspired by javascript's jQuery and uses similar API allowing selecting of HTML nodes through CSS selectors. This makes it easy for developers who are already familiar with jQuery to use PyQuery in Python.
Unlike jQuery, PyQuery doesn't support XPath selectors and relies entirely on CSS selectors though offers similar HTML parsing features like selection of HTML elements, their attributes and text as well as html tree modification.
PyQuery also comes with a http client (through requests
) so it can load and parse web URLs by itself.
Highlights
Example Use
from bs4 import BeautifulSoup
# this is our HTML page:
html = """
<head>
<title>Hello World!</title>
</head>
<body>
<div id="product">
<h1>Product Title</h1>
<p>paragraph 1</p>
<p>paragraph2</p>
<span class="price">$10</span>
</div>
</body>
"""
soup = BeautifulSoup(html)
# we can iterate using dot notation:
soup.head.title
"Hello World"
# or use find method to recursively find matching elements:
soup.find(class_="price").text
"$10"
# the selected elements can be modified in place:
soup.find(class_="price").string = "$20"
# beautifulsoup also supports CSS selectors:
soup.select_one("#product .price").text
"$20"
# bs4 also contains various utility functions like HTML formatting
print(soup.prettify())
"""
<html>
<head>
<title>
Hello World!
</title>
</head>
<body>
<div id="product">
<h1>
Product Title
</h1>
<p>
paragraph 1
</p>
<p>
paragraph2
</p>
<span class="price">
$20
</span>
</div>
</body>
</html>
"""
from pyquery import PyQuery as pq
# this is our HTML page:
html = """
<head>
<title>Hello World!</title>
</head>
<body>
<div id="product">
<h1>Product Title</h1>
<p>paragraph 1</p>
<p>paragraph2</p>
<span class="price">$10</span>
</div>
</body>
"""
doc = pq(html)
# we can use CSS selectors:
print(doc('#product .price').text())
"$10"
# it's also possible to modify HTML tree in various ways:
# insert text into selected element:
print(doc('h1').append('<span>discounted</span>'))
"<h1>Product Title<span>discounted</span></h1>"
# or remove elements
doc('p').remove()
print(doc('#product').html())
"""
<h1>Product Title<span>discounted</span></h1>
<span class="price">$10</span>
"""
# pyquery can also retrieve web documents using requests:
doc = pq(url='http://httpbin.org/html', headers={"User-Agent": "webscraping.fyi"})
print(doc('h1').html())