运维学python之爬虫工具篇(六)Pyquery的用法

news/2024/7/19 9:39:47 标签: javascript, 爬虫, 运维

1 简介

今天介绍的工具是Pyquery,没错,就是jquery的表哥,是用于python的jquery类库。pyquery允许您在xml文档上进行jquery查询。API类似于jquery。pyquery使用lxml进行快速xml和html操作。
Pyquery官方文档
jQuery中文在线手册

2 安装

pip安装

pip install pyquery

pycharm安装看以前的文章能找到(略)

3 初始化

您可以使用PyQuery类从一个字符串、一个lxml文档,从一个文件或一个url四种方式初始化,分别举例:

>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> import urllib
>>> d = pq("<html></html>") # 字符串
>>> d = pq(etree.fromstring("<html></html>"))  # lxml文档
>>> d = pq(url='http://google.com/') # url
>>> d = pq(filename=path_to_html_file) # 文件

4 快速开始

快速体验
以从一个文件方式初始化,文件名‘index.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"><span class="bold">third item</span></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>

编写程序如下:

# -*- coding: utf-8 -*-

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

运行结果如下:

    <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"><span class="bold">third item</span></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>

<class 'pyquery.pyquery.PyQuery'>
<class 'pyquery.pyquery.PyQuery'>
first item second item third item fourth item fifth item

属性操作

p = '<img src="http://static.firefoxchina.cn/img/201712/5_5a459650ed4440.jpg" class="21">'
doc = pq(p)
print(doc.attr('class', '21'))
print(doc.attr('src'))
print(doc.remove_attr('src'))
print(doc.remove_class('21'))
print(doc.add_class('22'))
print(doc.css('font-size', '16px'))
print(doc.css('backgroupd-color', 'red'))

结果:

<img src="http://static.firefoxchina.cn/img/201712/5_5a459650ed4440.jpg" class="21"/>
http://static.firefoxchina.cn/img/201712/5_5a459650ed4440.jpg
<img class="21"/>
<img class=""/>
<img class="22"/>
<img class="22" style="font-size: 16px"/>
<img class="22" style="font-size: 16px; backgroupd-color: red"/>

发现和jquery操作基本一样,只不过doc相当于$,而且通过上面例子也可以看出p一直是在原来结果上变化的。
DOM

p = '<p>I would like to say:</p>'
doc = pq(p)
a = doc.append('<b>Hello a</b>')
print(a)
b = doc.prepend('<b>Hello b</b>')
print(b)

DOM 操作也是与 jQuery 一样。
遍历

d = pq('<div><span>foo</span><span>bar</span></div>')
print([i.text() for i in d.items('span')])

通过items获取列表内容进行循环,结果如下:

['foo', 'bar']

api
更多内容api

其实要搞好这一章,还是需要去看看jquery,看完之后再回过头来看pyquery,会好很多。
部分内容摘自:崔庆才的个人博客

转载于:https://blog.51cto.com/linuxliu/2055848


http://www.niftyadmin.cn/n/992910.html

相关文章

BCD码、十六进制与十进制互转

在做嵌入式软件的设计中&#xff0c;经常会遇到十六进制、BCD码与十进制之间的转换&#xff0c;最近做M1卡的应用中&#xff0c;涉及了大量的十六进制、BCD码与十进制之间的转换。笔者通过对BCD码、十六进制 权的理解&#xff0c;轻松的实现了他们之间的互换。 #include #inclu…

在有html文本编辑控件退出时报错

html文本编辑控件的窗体上&#xff0c;退出时&#xff0c;报错&#xff0c;在开发环境中&#xff0c;项目直接中断退出 发现是这句话造成的 IntPtr ptr Marshal.GetIDispatchForObject(this.theSite); int i Marshal.Release(ptr); …

java8-08-自定义Collector-groupBy

[TOC] 声明 这一系列文章旨在帮助大家理解 Collector 的执行流程&#xff0c;至于实现的是否高效、是否优雅、是否合理等暂且不论。 现在来实现一个和 java.util.stream.Collectors#groupingBy() 一样效果的 GroupByCollector。 当然此处的实现肯定没有jdk那样灵活&#xff0c;…

运算符重载实例

1/// <summary> 2 /// 运算符重载实例 3 /// </summary>4publicclassOperatorOverloading5{ 6 7 public class AddClass1 8 { 9 public int val;1011 /// <summary>12 /// 运算符重载13 …

/是目录的分隔符,以/开头代表根目录。~/开头代表网站根目录。 ../表示上一级目录,./表示当前目录。...

/是目录的分隔符&#xff0c; 以/开头代表根目录。 ~/开头代表网站根目录。 ../表示上一级目录&#xff0c; ./表示当前目录。

一个好的C# SQLHelper类

代码 usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;namespaceAnalysisRuleFileDAL{publicclassSQLHelper {#region通用方法//数据连接池 privateSqlConnection con;///<summary&g…

【openGauss数据库】---设置开机自启动openGauss数据库服务

【openGauss数据库】---设置开机自启动openGauss数据库服务 &#x1f53b; 一、openGauss 自定义服务的配置文件了解&#x1f53b; 二、设置openGauss 开机自启动&#x1f53b; 三、总结—温故知新 &#x1f448;【上一篇】 &#x1f496;The Begin&#x1f496; 点点关注&am…

DataTemplate

DataTemplate作用是布局数据绑定 使用DataTemplate 同时完成样式布局和数据绑定 <Window.Resources><DataTemplate x:Key"PersonDataTemplate"> <Grid> <Grid.RowDefinitions> <RowDefinition Height"*"></RowDefinition…