jquery Pyquery Library Usage summary in Python _python

Source: Internet
Author: User

The Pyquery library is a python implementation of jquery that can be used to parse HTML Web page content using the following methods:

Copy Code code as follows:

From pyquery import Pyquery as PQ

1, can load an HTML string, or an HTML file, or a URL address, example:
Copy Code code as follows:

D = PQ ("D = PQ (Filename=path_to_html_file)
D = PQ (url= ' http://www.baidu.com ') # Here URL must be written full

2, HTML () and text ()--get the corresponding HTML block or block, example:
Copy Code code as follows:

p = PQ ("P (' head '). html () # Back to <title>hello</title>
P (' head '). Text () # return Hello

3, according to the HTML tag to get the elements, example:
Copy Code code as follows:

D = PQ (' <div><p>test 1</p><p>test 2</p></div> ')
D (' P ') # return [<p>,<p>]
Print d (' P ') # returns to <p>test 1</p><p>test 2</p>
Print d (' P '). html () # Return to test 1

Note: When more than one element is fetched, the HTML (), text () method returns only the corresponding block of content for the first element

4, EQ (index)--to get the specified element according to the given index number

For example, if you want to get the contents of the second P tag, you can:

Copy Code code as follows:

Print d (' P '). EQ (1). html () # Return to test 2

5, filter ()--according to the class name, id name to get the specified element, example:

Copy Code code as follows:

D = PQ ("<div><p id= ' 1 ' >test 1</p><p class= ' 2 ' >test 2</p></div>")
D (' P '). Filter (' #1 ') # return [<p#1>]
D (' P '). Filter ('. 2 ') # returns [<p.2>]

6, find ()--look for nested elements, example:
Copy Code code as follows:

D = PQ ("<div><p id= ' 1 ' >test 1</p><p class= ' 2 ' >test 2</p></div>")
D (' div '). Find (' P ') # return [<p#1>, <p.2>]
D (' div '). Find (' P '). EQ (0) #返回 [<p#1>]

7, directly according to the class name, id name to get the element, example:
Copy Code code as follows:

D = PQ ("<div><p id= ' 1 ' >test 1</p><p class= ' 2 ' >test 2</p></div>")
D (' #1 '). html () # Return to test 1
D ('. 2 '). html () # Return to test 2

8, get the attribute value, example:
Copy Code code as follows:

D = PQ ("<p id= ' my_id ' ><a href= ' http://hello.com ' >hello</a></p>")
D (' a '). attr (' href ') # return to Http://hello.com
D (' P '). attr (' id ') # return my_id

9, modify the attribute value, example:
Copy Code code as follows:

D (' a '). attr (' href ', ' http://baidu.com ')

10, AddClass (value)--Add a class for the element, example:

Copy Code code as follows:

D = PQ (' <div></div> ')
D.addclass (' My_class ') # return [<div.my_class>]

11, Hasclass (name) #返回判断元素是否包含给定的类, example:
Copy Code code as follows:

D = PQ ("<div class= ' My_class ' ></div>")
D.hasclass (' My_class ') # returns True

12, children (selector=none)--Get child elements, example:
Copy Code code as follows:

D = PQ ("<span><p id= ' 1 ' >hello</p><p id= ' 2 ' >world</p></span>")
D.children () # return [<p#1>, <p#2>]
D.children (' #2 ') # return [<p#2>]

13, parents (Selector=none)--Get the parent element, example:
Copy Code code as follows:

D = PQ ("<span><p id= ' 1 ' >hello</p><p id= ' 2 ' >world</p></span>")
D (' P '). Parents () # return [<span>]
D (' #1 '). Parents (' span ') # return [<span>]
D (' #1 '). Parents (' P ') # return []

14, Clone ()--Return a copy of a node

15, empty ()--Remove node content

16, Nextall (Selector=none)--return all the following elements block, example:

Copy Code code as follows:

D = PQ ("<p id= ' 1 ' >hello</p><p id= ' 2 ' >world</p> ')"
D (' P:first '). Nextall () # return [<p#2>, ]
D (' P:last '). Nextall () # return []

17, Not_ (selector)--Returns an element that does not match the selector, for example:
Copy Code code as follows:

D = PQ ("<p id= ' 1 ' >test 1</p><p id= ' 2 ' >test 2</p>")
D (' P '). Not_ (' #2 ') # return [<p#1>]

More content, refer to official website http://packages.python.org/pyquery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.