Python模块pyquery应用技巧

公众号 yk 坤帝

后台回复 pyquery 获取全部源代码

1. 初始化部分

1.1 字符串初始化

1.2 URL初始化

1.3 文件初始化

2. 基本CSS选择器

3. 查找元素

3.1 查找子元素

3.2 查找父元素

3.3 查找兄弟元素

4. 遍历单个元素

5. 获取属性,文本,HTML

5.1 获取属性

5.2 获取文本

5.3 获取HTML

6. DOM操作

6.1 addClass、removeClass

6.2 attr、css

6.3 remove

7. 伪类选择器

1. 初始化部分

1.1 字符串初始化

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li'))

1.2 URL初始化

from pyquery import PyQuery as pq
doc = pq(url='http://www.baidu.com')
print(doc('head'))

1.3 文件初始化

from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))

2. 基本CSS选择器

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('#container .list li'))

3. 查找元素

3.1 查找子元素

公众号 yk 坤帝

后台回复 pyquery 获取全部源代码

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
print(type(items))
print(items)
lis = items.find('li')
print(type(lis))
print(lis)
lis = items.children()
print(type(lis))
print(lis)

3.2 查找父元素

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
container = items.parent()
print(type(container))
print(container)
html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
parents = items.parents()
print(type(parents))
print(parents)
parent = items.parents('.wrap')
print(parent)

3.3 查找兄弟元素

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')
print(li.siblings())
html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')
print(li.siblings('.active'))

4. 遍历单个元素

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
lis = doc('li').items()
print(type(lis))
for li in lis:
    print(li)

5. 获取属性,文本,HTML

5.1 获取属性

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.attr('href'))
print(a.attr.href)

5.2 获取文本

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.text())

5.3 获取HTML

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
print(li.html())

6. DOM操作

6.1 addClass、removeClass

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.removeClass('active')
print(li)
li.addClass('active')
print(li)

6.2 attr、css

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.attr('name', 'link')
print(li)
li.css('font-size', '14px')
print(li)

6.3 remove

html = '''
Hello, World

This is a paragraph.

''' from pyquery import PyQuery as pq doc = pq(html) wrap = doc('.wrap') print(wrap.text()) wrap.find('p').remove() print(wrap.text())

7. 伪类选择器

html = '''

'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('li:first-child')
print(li)
li = doc('li:last-child')
print(li)
li = doc('li:nth-child(2)')
print(li)
li = doc('li:gt(2)')
print(li)
li = doc('li:nth-child(2n)')
print(li)
li = doc('li:contains(second)')
print(li)

公众号 yk 坤帝

后台回复 pyquery 获取全部源代码

你可能感兴趣的:(笔记,项目,python,pyquery,项目,应用)