Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 27, 2017 01:00 pm

PyQuery: Python's jQuery

In this tutorial, you'll have a look at PyQuery, a Python library which allows you to make jQuery queries on XML documents. Syntactically it's quite similar to jQuery, and if you are familiar with jQuery it should be easier to follow.

Getting Started With PyQuery

To get started with PyQuery, install the Python package using PIP.

Once you have installed PyQuery, import it into the Python program.

Let's start with a basic example of how it works. Consider the following HTML: 

Pass the input XML to the PyQuery object and you should be able to apply jQuery style queries to it.

Assume divMain as a jQuery object and print the HTML content of the div.

Save the changes and try running the program. You should have the following output:

To access the Hello World text from the inside the span, the code would be:

Save the changes and try running the code. You should have the following output:

Attributes Manipulation Using PyQuery

Now let's have a look at how to read the attributes using the PyQuery library. Assume that you have an HTML element as shown:

Use the PyQuery library to read the above HTML content.

Let's try to access the ID attribute of the ulMain ul.

Save the above changes and try running the Python program. You should have ID ulMain printed on the terminal.

You saw how to access the ID attribute of the ul ulMain. Let's try to set a class name for the same ulMain element. To specify a class name attribute, you need to specify the attribute name and its corresponding value as shown:

Print the ulMain object and you should have the element with the class attribute.

You can also add and remove classes directly without using the attr method. To add a class to the element, you can make use of the method addClass

To remove an existing class from the element, you can make use of the removeClass method.

Handling CSS Using PyQuery

Apart from attributes, the element would have some CSS properties. To add or modify the CSS properties, you can use the css method and specify the properties. Let's say that you want to specify the height in the style of the ul ulMain. The following code would add the required style properties to the element.

Save the above changes and try executing the Python program. You should have the ul ulMain printed along with the new style.

To add multiple styles to the element, you can specify them as shown:

Run the program and you should have the styles added to the ulMain.

Creating & Appending Elements

During dynamic element creation, you are required to create new elements and append them to the existing parent element where they'll be rendered. Let's have a look at how to create and append elements using PyQuery

Assume you have a main container div called divMain.

Let's create a new span element using PyQuery.

Add some CSS properties to the span.

PyQuery provides a method to add elements to existing elements. You can use the append method to append the span element to the div divMain. Here is the code:

Save the changes and run the program. You should be able to see the divMain printed with the newly created span appended to it.

You used the append method to append the span to the div. You have another alternative method called appendTo which would append the nodes to value. In the above case you can use the method like so:

Finding Elements Using PyQuery

PyQuery provides methods to find children, the next elements, the closest elements, etc. It also provides methods to filter and find elements inside a particular node.

Assume that you have a particular piece of HTML as shown:

Add the following HTML to the PyQuery object:

Let's use PyQuery to find the children of the div divMain. Add the following line of code to print the children of divMain.

On running the program, you should have the following output:

To find the closest element to an element, you can use the method closest. To find the closest div element to the span, the code would be:

The above command would return the following output:

A find method is also available to find the elements. For example, to find a span inside the divMain, you need to call the method as shown:

The above command would return the span.

Insert an Element Inside HTML

While append does the work of adding elements to the existing elements, sometimes you need to insert elements after or before certain elements. PyQuery provides a method to insert elements before or after other elements.

Let's define a new paragraph element to be inserted after the span inside the divMain div.

Call the insertAfter method on the paragraph p element to insert it after the span.

Save the above changes and run the Python program. You should have the following output:

Similarly, you have the insertBefore method, which inserts before the element. Modify the code as shown below to use the insertBefore method: 

Save the changes and run the program. You should be able to see the following output on the terminal:

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. Do let us know your suggestions in the comments below.


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code