Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does pyquery get the element or text information you want

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces pyquery how to get the elements or text information you want, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Hello, everyone, today we share how pyquery gets the elements or text information you want.

We mainly give some case that are commonly used in engineering development. Later, I will use what we have learned today to capture the interesting fund data on the Tiantian Fund online.

1. You can load a HTML string, or a HTML file, or an url address

D=pq ("hello")

D=pq (filename=path_to_html_file)

D=pq (url=' http://www.baidu.com') Note: url seems to have to be fully written here.

2.html () and text ()-- get the corresponding HTML block or text block

P=pq ("hello")

P ('head') .html () # returns hello

P ('head') .text () # returns hello

3. Get elements based on HTML tags

D=pq ('

Test 1

Test 2

')

D ('p') # returns [

]

Print d ('p') # returns

Test 1

Test 2

Print d ('p') .html () # returns test 1

Note: when more than one element is obtained, the html () and text () methods only return the corresponding content block of the first element.

4.eq (index)-- gets the specified element based on the given index number

Following the example above, if you want to get the contents of the second p tag, you can:

Print d ('p') .eq (1) .html () # returns test 2

5.filter ()-- gets the specified element based on the class name and id name, for example:

D=pq ("test 1"

Test 2

")

D ('p') .filter ('# 1') # returns []

D ('p') .filter ('.2') # returns []

6.find ()-- find nested elements

D=pq ("test 1"

Test 2

")

D ('div') .find (' p') # returns [,]

D ('div') .find (' p') .eq (0) # returns []

7. Get the element directly based on the class name and id name

D=pq ("test 1"

Test 2

")

D ('# 1') .html () # returns test 1

D ('.2') .html () # returns test 2

8. Get attribute value

D=pq ("hello"

")

D ('a') .attr ('href') # returns http://hello.com

D ('p') .attr ('id') # returns my_id

9. Modify attribute value

D ('a') .attr ('href',' http://baidu.com') changes the href attribute to baidu

10.addClass (value)-- add classes to elements

D=pq ('')

D.addClass ('my_class') # returns []

11.hasClass (name) # returns to determine whether the element contains the given class

D=pq ("")

D.hasClass ('my_class') # returns True

12.children (selector=None)-- get child elements

D=pq ("hello"

World

")

D.children () # returns [,]

D.children ('# 2') # returns []

13.parents (selector=None)-- get the parent element

D=pq ("hello"

World

")

D ('p') .parents () # returns []

D ('# 1') .parents ('span') # returns []

D ('# 1') .parents ('p') # returns []

14.clone ()-returns a copy of a node

15.empty ()-- removes node content

16.nextAll (selector=None)-returns all subsequent element blocks

D=pq ("hello"

World

")

D ('pbank first') .nextAll () # returns [

]

D ('pvirtual last`) .nextAll () # returns [

]

17.Not _ (selector)-returns the element of the mismatched selector

D=pq ("test 1"

Test 2

")

D ('p') .not_ ('# 2') # returns []

18. Traverse:

Traversal uses the items method to return a list of objects, or use lambda

#! / usr/bin/python

"

@ File: pyquery_demo.py

Time: 16:46 on 2019-8-31

@ Author: haishiniu

@ Software: PyCharm

"

From pyquery import pyquery as pq

Doc = pq ('h2h3')

List = doc ('li')

For li in list.items ():

Print (li.html) about how pyquery get the elements or text information you want to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report