PyQuery

目前最新的版本是1.3,基于最新版本进行介绍。

主要根据PyQuery的官方文档进行了更新。

from pyquery import PyQuery as pq
from lxml import etree
import urllib
doc=pq('<p id="hello" class="hello"></p>')#解析文档的基本方法
p=doc('p')#获取p标签
print(p.attr('id'))#获取p标签的id属性的值
p.attr('id','plop')#改变p标签的id属性的值为plop
print(p.attr('id'))
p.attr.id='snow'#使用python的方式改变id属性
print(p.attr.id)
p.attr['id']='ola'
print(p.attr['id'])
p.attr(id='hello',class_='hello2')
print(p)
hello
plop
snow
ola
<p id="hello" class="hello2"/>

CSS内容

p.add_class('toto')#增加class内容
# print(p)
p.toggle_class('titi toto')#切换class内容
# print(p)
p.remove_class('titi')#移除class
# print(p)
#添加style,以font-size为例
p.css.font_size='16px'
p.css['font-size']='16px'
p.css={'font-size':'17px'}

Manipulating

#向标签内容的尾部添加一些string
p.append(' check out <a href="http://www.baidu.com">百度一下</a>')
#pp()
#也可以向标签的头部添加
p.prepend('hello again! ')
#pp()
#或者使用这两个命令添加到其他文档或者标签的内容中
d=pq('<html><body><div id="test"><a href="http://python.org">python</a> !</div></body></html>')
p.prepend_to(d('#test'))#将p的内容添加到d的id为test的标签里面,占据最开始的位置
#print(d('#test').html())#打印出id为test的标签的内容 p.insert_after(d('#test'))#把p标签放在id为test的标签后面
#print(d('body').html())
p.insert_before(d('#test'))#把p标签插入到id为test的标签前面
#print(d('body').html())#可以看出来,p插入到了id为test的标签的前面 #移除一个元素
d=pq('<html><body><p id="id">Yeah!</p><p>python rocks !</p></div></html>')
d.remove('p#id')#移除了p标签的id属性
#print(d('body').html()) #将标签的内容移除
d('p').empty()
#print(d('p')) #可以将两个PyQuery对象连接起来:
print(pq('<div>Yeah !</div>').add_class('myclass') + pq('<b>cool</b>'))

Traversing

d = pq('<p id="hello1" class="test1"><a>1</a></p><p id="hello2" class="test2"><a>2</a></p>')

#print(d('p').filter('.test1'))#按类选择,class名为test1的p标签。
#print(d('p').filter('#hello2'))#按id选择,id名为hello2的p标签
#print(d('p').eq(0))#第一个p标签
#print(d('p').eq(1))#第二个p标签 """Return PyQuery of only the element with the provided index:
>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>')
>>> d('p').eq(0)
[<p.hello>]
>>> d('p').eq(1)
[<p>]
>>> d('p').eq(2)
[]
""" #查询嵌套的元素
#print(d('p').find('a'))
#print(d('p').eq(1).find('a'))

API

http://pythonhosted.org/pyquery/api.html

请参考官方文档的介绍。

Scraping

#PyQuery可以使用url载入html文档,默认使用python的urllib库
print(pq('http://www.baidu.com'))
#如果安装了request库,也可以使用并且可以使用requests的大多参数
pq('http://duckduckgo.com/', headers={'user-agent': 'pyquery'})
pq('https://duckduckgo.com/', {'q': 'foo'}, method='post', verify=True)

如果您觉得感兴趣的话,可以添加我的微信公众号:一步一步学Python

爬虫入门【5】PyQuery简介的更多相关文章

  1. Python网络爬虫入门篇

    1.  预备知识 学习者需要预先掌握Python的数字类型.字符串类型.分支.循环.函数.列表类型.字典类型.文件和第三方库使用等概念和编程方法. 2. Python爬虫基本流程 a. 发送请求 使用 ...

  2. Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分

    1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...

  3. Python 入门之Python简介

    Python 入门之Python简介 1.Python简介: (1) Python的出生: ​ python的创始人为吉多·范罗苏姆(Guido van Rossum)(中文名字:龟叔).1989年的 ...

  4. Python基础及爬虫入门

    **写在前面**我们在学习任何一门技术的时候,往往都会看很多技术博客,很多程序员也会写自己的技术博客.但是我想写的这些不是纯技术博客,我暂时也没有这个能力写出 Python 或者爬虫相关的技术博客来. ...

  5. Python爬虫入门(二)之Requests库

    Python爬虫入门(二)之Requests库 我是照着小白教程做的,所以该篇是更小白教程hhhhhhhh 一.Requests库的简介 Requests 唯一的一个非转基因的 Python HTTP ...

  6. 爬虫入门五 gooseeker

    title: 爬虫入门五 gooseeker date: 2020-03-16 16:00:00 categories: python tags: crawler gooseeker是一个简单的爬虫软 ...

  7. 爬虫入门四 re

    title: 爬虫入门四 re date: 2020-03-14 16:49:00 categories: python tags: crawler 正则表达式与re库 1 正则表达式简介 编译原理学 ...

  8. 爬虫入门三 scrapy

    title: 爬虫入门三 scrapy date: 2020-03-14 14:49:00 categories: python tags: crawler scrapy框架入门 1 scrapy简介 ...

  9. 爬虫入门二 beautifulsoup

    title: 爬虫入门二 beautifulsoup date: 2020-03-12 14:43:00 categories: python tags: crawler 使用beautifulsou ...

  10. Python简单爬虫入门三

    我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...

随机推荐

  1. vertex buffer 数据结构 如何读vb的memory pool

    vertex attribute (declaration)    vertex stream (memory pool) 这两部分 通过attribute 里面对memory的描述把两部分 vbo ...

  2. Myeclipse 编译等级

    1.Java compiler level does not match the version of the installed Java project facet. 问题描述:编译等级不匹配 解 ...

  3. crontab配置

    1.命令功能 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔的单位可以是分钟.小时.日.月.周及以上的任意组合.这个命令非常适合周期性的 ...

  4. How to Clear setInterval() without Knowing the ID

    ProblemDeclaring a setInterval() without keeping a reference to it (which is returned from the funct ...

  5. pl/sql(2)

    1.存储过程 (1)存储过程的创建及改动 语法: CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | I ...

  6. 仿苹果电脑任务栏菜单&&拼图小游戏&&模拟表单控件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. ES7新特性

    Array.prototype.includes Array.prototype.includes用法都容易和简单.它是一个替代indexOf,开发人员用来检查数组中是否存在值,indexOf是一种尴 ...

  8. 51单片机 | 使用D/A转换器实现三角波发生器

    ———————————————————————————————————————————— D/A转换器 CS=0.ILE=1时,WR1信号有效时将数据总线上的信号写入8位输入锁存器 XFER=0时,W ...

  9. PHP和Java的主要区别有哪些?哪个最适合Web开发语言?

    一.前言 PHP和Java都是现在比较流行的二种编程语言. 对于许多新手来说,都会思考如果学的时候,该学哪种语言呢?下面这篇文章给大家整理两者的区别以及一些选择建议,一起来看看吧. 二.简介 PHP与 ...

  10. .NET CORE 2.0小白笔记(五):配置的热更新、配置的框架设计

    配置的热更新 什么是热更新:一般来说,我们创建的项目都无法做到热更新:即项目无需重启,修改配置文件后读取到的信息就是修改配置之后的 我们只需要吧项目中用到的IOptions改成IOptionsSnap ...