Table of Contents
Attribute Manipulation Using PyQuery
Creating, Removing, & Appending Elements
Finding Elements Using PyQuery
Extracting Content From a Webpage
Wrapping It Up
Home Backend Development Python Tutorial PyQuery: Python's jQuery

PyQuery: Python's jQuery

Mar 01, 2025 am 10:22 AM

In this tutorial, you'll have a look at PyQuery object is similar to what you get with $() when using the jQuery library. Just like the html() method in PyQuery, where you will be able to get or set the HTML content of the selected element.

Currently, the webpage object is representative of the whole document, so it returns the markup of the entire page:

print(webpage.html())<br><br>'''<br><head><br><meta charset="utf-8"/><br><title>A Simple Webpage</title><br><meta name="viewport" content="width=device-width, initial-scale=1"/><br></head><br><br><body><br>    <p>Hello <b>world</b>! This is a basic webpage.</p><br>    <p>Here is a list of some <i>random</i> words:</p><br>    <ul ><br>        <li>Impedimenta</li><br>        <li>Decompensation</li><br>        <li>Tergiversation</li><br>        <li>Transcendentalism</li><br>        <li>Polyphiloprogenitive</li><br>    </ul><br></body><br>'''<br>
Copy after login
Copy after login

Let's say you want to get the markup of the first webpage object. Here is an example:

print(webpage("p").html())<br><br>'''<br>Hello <b>world</b>! This is a basic webpage.<br>'''<br>
Copy after login
Copy after login

Now take a look at the following code, where we will first set the HTML for our selector using the html() method.

from pyquery import PyQuery as pq<br><br>webpage = pq(filename = 'document.html')<br><br>print(webpage("p").html())<br>'''<br>Hello <b>world</b>! This is a basic webpage.<br>'''<br><br>webpage("p").html("Hello <b>world</b>! I have changed this paragraph.")<br><br>print(webpage("p").html())<br>'''<br>Hello <b>world</b>! I have changed this paragraph.<br>'''<br>
Copy after login
Copy after login

As you can see, it was very easy for us to manipulate the HTML of particular tags. Let's see what else we can change.

Attribute Manipulation Using PyQuery

PyQuery tries to mirror the jQuery API as closely as possible. This means that you get access to an attribute method called class attribute from the list. We will also use the attr() method to add a set of classes to our attr() method in PyQuery also sets the attribute value for all matching elements instead of the first one.

How can we apply the classes only to the first eq() method to get the element at a particular index, as shown below.

webpage("p").eq(0).attr("class", "greeting hello-message")<br>
Copy after login
Copy after login

If you are primarily looking to manipulate classes of your elements, you can also consider using the removeClass() methods, which will add or remove a CSS class respectively. You can also use the method names remove_class() if you are more comfortable working with underscore notation.

Here is an example:

webpage("p").eq(0).attr("class", "greeting hello-message")<br># 

Hello world! This is a basic webpage.



webpage("p").eq(0).remove_class("greeting")
#

Hello world! This is a basic webpage.



webpage("p").eq(0).add_class("first-message")
#

Hello world! This is a basic webpage.


Copy after login
Copy after login

You can also get rid of any attributes assigned to an element by using the add_attr() method as that functionality is served by font-size to css() method in PyQuery is similar to the one in jQuery. After updating the styles, we saved the new markup to a file called updated_markup.html. You can also do the same after making a variety of changes to the markup.

Creating, Removing, & Appending Elements

You might recall that our sample HTML document contains a list of words. Can we expand the list of words? Of course we can. All you need to do is use the prepend() methods. The prepend() method will prepend the passed value to the calling node. Here is an example:

from pyquery import PyQuery as pq<br><br>webpage = pq(filename = 'document.html')<br><br>print(webpage("ul"))<br>'''<br><ul ><br>    <li>Impedimenta</li><br>    <li>Decompensation</li><br>    <li>Tergiversation</li><br>    <li>Transcendentalism</li><br>    <li>Polyphiloprogenitive</li><br></ul><br>'''<br><br>webpage("ul").append("<li>Myrmecophilous</li>")<br>webpage("ul").prepend("<li>Anagnorisis</li>")<br><br>print(webpage("ul"))<br>'''<br><ul ><br>    <li>Anagnorisis</li><br>    <li>Impedimenta</li><br>    <li>Decompensation</li><br>    <li>Tergiversation</li><br>    <li>Transcendentalism</li><br>    <li>Polyphiloprogenitive</li><br>    <li>Myrmecophilous</li><br></ul><br>'''<br>
Copy after login

Another option that you have for appending and prepending elements is the use of the prepend_to() methods. The prepend_to() method will prepend your calling node to the passed node. However, remember that you cannot simply call these methods on a string. You will have to wrap them in a PyQuery object for the call to work, as shown below:

print(webpage.html())<br><br>'''<br><head><br><meta charset="utf-8"/><br><title>A Simple Webpage</title><br><meta name="viewport" content="width=device-width, initial-scale=1"/><br></head><br><br><body><br>    <p>Hello <b>world</b>! This is a basic webpage.</p><br>    <p>Here is a list of some <i>random</i> words:</p><br>    <ul ><br>        <li>Impedimenta</li><br>        <li>Decompensation</li><br>        <li>Tergiversation</li><br>        <li>Transcendentalism</li><br>        <li>Polyphiloprogenitive</li><br>    </ul><br></body><br>'''<br>
Copy after login
Copy after login

As you can see, we get the same output. You can also remove nodes from your document by simply calling the children() and children() method returns all the elements that are direct children of the calling node. In our case, this means all the list elements. After that, we use the li tags to append them to our now empty unordered list.

Finding Elements Using PyQuery

There is a good chance that you will be working with HTML documents in order to extract some data from them. Now, before you can extract this data from any element, you will need to locate or find the element.

You can simply use the closest() method to look for elements if you are interested in searching through the ancestors of that particular selector.

print(webpage("p").html())<br><br>'''<br>Hello <b>world</b>! This is a basic webpage.<br>'''<br>
Copy after login
Copy after login

We have already covered the siblings() method. Other similar methods that you can use are prev_all(), which will give you all the siblings that come next or the siblings that came before respectively. Here is an example:

from pyquery import PyQuery as pq<br><br>webpage = pq(filename = 'document.html')<br><br>print(webpage("p").html())<br>'''<br>Hello <b>world</b>! This is a basic webpage.<br>'''<br><br>webpage("p").html("Hello <b>world</b>! I have changed this paragraph.")<br><br>print(webpage("p").html())<br>'''<br>Hello <b>world</b>! I have changed this paragraph.<br>'''<br>
Copy after login
Copy after login

Extracting Content From a Webpage

Do you remember when I told you at the beginning of the tutorial that PyQuery can accept input from multiple sources such as a string, a file, or even a URL?

In this section, we will let PyQuery get its markup from a page about Python on Wikipedia. The webpage contains a lot of information about Python. We will try to extract some of it for our consumption. Let's see if we can get all the h2<code>h2 level headings to keep things simple.

Believe it or not, you only need five lines of code to get your heading text.

webpage("p").eq(0).attr("class", "greeting hello-message")<br>
Copy after login
Copy after login

You might have noticed that I used the selector h2 span.mw-headline instead of using h2. This is because simply using h2 was giving me some additional headings that were not part of the main content. You will also have to do a similar analysis of webpages yourself before determining the appropriate selector to use for extracting the information.

I have already written a tutorial on the Requests module in Python where we used the module to download images. One limitation of the example I included there was that we were hard-coding the path of the image. Let's use the PyQuery library to extract the image paths from a webpage and then feed them to the requests module to download. I will be using the Wikipedia page about the United States for this example:

webpage("p").eq(0).attr("class", "greeting hello-message")<br># 

Hello world! This is a basic webpage.



webpage("p").eq(0).remove_class("greeting")
#

Hello world! This is a basic webpage.



webpage("p").eq(0).add_class("first-message")
#

Hello world! This is a basic webpage.


Copy after login
Copy after login

We don't want to download images of the UI icons, etc. That's why I used a more specific selector to extract our images. I get the image file name by taking the last part of the image path after splitting it along the / character. Here are some of the images that I was able to extract:

PyQuery: Python's jQuery

Wrapping It Up

In this tutorial, you saw how to get started with PyQuery, a Python library which allows you to make jQuery queries on XML documents. You saw how to manipulate the attributes and CSS styles of the HTML elements. 

You learnt how to create and append elements to existing elements and insert new elements before and after elements. What you saw in this tutorial is just the tip of the iceberg, and there is a lot more that this library has to offer.

For more detailed information on using this library, I would recommend reading the official documentation.

The above is the detailed content of PyQuery: Python's jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles