>

Python打字与印刷轻松三角形,Python爬虫之PyQuery解

- 编辑:www.bifa688.com -

Python打字与印刷轻松三角形,Python爬虫之PyQuery解

打印简单三角形:

本文为笔者根据教程记录笔记,仅供学习使用

public class ThreadDemo {

#!/usr/bin/python

i = 1
while i <= 5:
#打印空格
    j = 1
    while j <= 5-i:
        print(' ', end="")
        j  = 1    
#打印"*"
    a = 1
    while a <= 2*i-1:
        print('*', end="")
        a  = 1
    print('')
    i  = 1

PyQuery解析库跟BeautifulSoup解析库一样都是方便我们解析文档的,但是PyQuery解析库跟JS的框架JQuery很相似,如果之前接触过JQuery的话,再学习这个就没有什么学习成本了。

public static void main(String[] args) {
new TestThread().run();
/////////顺序执行///////////
for(int i = 0; i<10;i  ){
    System.out.println("主函数main 在运行:" i);
}
}

运行结果:

1.初始化

  • 字符串初始化
html = '''
<div>
    <ul>
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html">third item</a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li'))

运行结果:

图片 1

运行结果图

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

运行结果:

图片 2

运行结果图

  • 文件初始化
from pyquery import PyQuery as pq
doc = pq(filename='demo.html')
print(doc('li'))

运行结果:

图片 3

运行结果图

}
class TestThread
{

图片 4

2.基本CSS选择器

html = '''
<div id="container">
    <ul class="list">
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html">third item</a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('#container .list li'))

运行结果:

图片 5

运行结果图

public void run() {
    for (int i = 0; i < 10; i  ) {
        System.out.println("TestThread运行中:" i);
    }
}
i = 1
while i <= 5:
    j = 1
    while j <= i:
        print('* ', end='')
        j  = 1
    print('')
    i  = 1
i = 1
while i <= 4:
    j = 4
    while j >= i:
        print('* ', end='')
        j -= 1
    print('')
    i  = 1

3.查找元素

  • 子元素
html = '''
<div id="container">
    <ul class="list">
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html">third item</a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
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)

运行结果:

图片 6

运行结果图

lis = items.children()
print(type(lis))
print(lis)

运行结果:

图片 7

运行结果图

lis = items.children('.active')
print(lis)

运行结果:

图片 8

运行结果图

  • 父元素
html = '''
<div id="container">
    <ul class="list">
         <li class="item-0">first item</li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-0 active"><a href="link3.html">third item</a></li>
         <li class="item-1 active"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a></li>
     </ul>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
container = items.parent()
print(type(container))
print(container)

运行结果:

图片 9

运行结果

html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
parents = items.parents()
print(type(parents))
print(parents)

运行结果:

图片 10

运行结果图

parent = items.parents('.wrap')
print(parent)

运行结果:

图片 11

运行结果图

  • 兄弟元素
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')
print(li.siblings())

运行结果:

图片 12

运行结果图

html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.list .item-0.active')
print(li.siblings('.active'))

运行结果:

图片 13

运行结果图

}

运行结果:

4.遍历

  • 单个元素
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)

运行结果:

图片 14

运行结果图

  • 多个元素
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
lis = doc('li').items()
print(type(lis))
for li in lis:
    print(li)

运行结果:

图片 15

运行结果图

1.运行结果
TestThread运行中:0
TestThread运行中:1
TestThread运行中:2
TestThread运行中:3
TestThread运行中:4
TestThread运行中:5
TestThread运行中:6
TestThread运行中:7
TestThread运行中:8
TestThread运行中:9
主函数main 在运行:0
主函数main 在运行:1
主函数main 在运行:2
主函数main 在运行:3
主函数main 在运行:4
主函数main 在运行:5
主函数main 在运行:6
主函数main 在运行:7
主函数main 在运行:8
主函数main 在运行:9

图片 16

5.获取信息

  • 获取属性
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
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)

运行结果:

图片 17

运行结果图

  • 获取文本
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
a = doc('.item-0.active a')
print(a)
print(a.text())

运行结果:

图片 18

运行结果图

  • 获取HTML
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
print(li.html())

运行结果:

图片 19

运行结果图

Python语法和C语言很大共同点,但是我不熟练,练练。

6.DOM操作

  • addClass、removeClass
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
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)

运行结果:

图片 20

运行结果图

  • attr、css
html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
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)

运行结果:

图片 21

运行结果图

  • remove
html = '''
<div class="wrap">
    Hello, World
    <p>This is a paragraph.</p>
 </div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
wrap = doc('.wrap')
print(wrap.text())
wrap.find('p').remove()
print(wrap.text())

运行结果:

图片 22

运行结果图

  • 其他DOM方法
    http://pyquery.readthedocs.io/en/latest/api.html

public class ThreadDemo {

7.伪类选择器

html = '''
<div class="wrap">
    <div id="container">
        <ul class="list">
             <li class="item-0">first item</li>
             <li class="item-1"><a href="link2.html">second item</a></li>
             <li class="item-0 active"><a href="link3.html">third item</a></li>
             <li class="item-1 active"><a href="link4.html">fourth item</a></li>
             <li class="item-0"><a href="link5.html">fifth item</a></li>
         </ul>
     </div>
 </div>
'''
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)

运行结果:

图片 23

运行结果图

public static void main(String[] args) {
    //此处是start  开启线程的意思 。。 如果 还是用run  跟初始没改的一样
new TestThread().start();
/////////顺序执行///////////
for(int i = 0; i<10;i  ){
    System.out.println("主函数main 在运行:" i);
}
}

}

class TestThread extends Thread

{

@Override
public void run() {
    for (int i = 0; i < 10; i  ) {
        System.out.println("TestThread运行中:" i);
    }
}

}
2.运行结果
主函数main 在运行:0
主函数main 在运行:1
主函数main 在运行:2
TestThread运行中:0
TestThread运行中:1
TestThread运行中:2
TestThread运行中:3
主函数main 在运行:3
主函数main 在运行:4
主函数main 在运行:5
主函数main 在运行:6
主函数main 在运行:7
主函数main 在运行:8
主函数main 在运行:9
TestThread运行中:4
TestThread运行中:5
TestThread运行中:6
TestThread运行中:7
TestThread运行中:8
TestThread运行中:9

本文由必发88手机版发布,转载请注明来源:Python打字与印刷轻松三角形,Python爬虫之PyQuery解