python网络爬虫系列教程——python中pyquery库应用全解
来源:互联网 发布:asp与php哪个好 编辑:程序博客网 时间:2024/06/07 03:35
全栈工程师开发手册 (作者:栾鹏)
python教程全解
python网络爬虫lxml库的应用全解。
在线安装方法:cmd中输入”pip install pyquery”
离线安装,下载pyquery库点击下载
python库的安装请参考Python库的安装与卸载
具体讲解废话太多,看代码注释最快。
python2.7、python3.6中代码
#coding:utf-8#网络爬虫库pyquery的应用,以下代码同时支持python2和python3from pyquery import PyQuery as pqprint(u'=====================初始化====================')doc = pq("<html></html>") #传入html代码#from lxml import etree#doc = pq(etree.fromstring("<html></html>")) #可以首先用lxml 的 etree 处理一下代码doc = pq('http://www.baidu.com') #传入网址text = '''<html><body><div> <ul> <li class="item-0"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> </li></ul> </div></body></html>'''#将字符串写入文件fh = open('test.html', 'w')fh.write(text)fh.close()doc = pq(filename='test.html') #传入本地文件print(doc.html()) #获取元素的内部html代码print(type(doc)) #返回类型是PyQueryli = doc('li') #获取所有的li元素print(type(li)) #返回类型依然是PyQuery,可以进行二次筛选print(li.text()) #获取li的内部文本print(u'=====================属性====================')p = pq('<p id="hello" class="hello"></p>')('p') #创建dom树后获取标签print(p.attr("id")) #读取属性值print(p.attr("id", "plop")) #设置属性值print(p.attr("id", "hello")) #设置属性值print(p.addClass('beauty')) #添加classprint(p.removeClass('hello')) #去除classprint(p.css('font-size', '16px')) #设置css值print(p.css({'background-color': 'yellow'})) #通过列表设置cssprint(u'=====================DOM====================')print(p.append(' check out <a href="http://reddit.com/r/python"><span>reddit</span></a>')) #在内部原有html代码后添加代码print(p.prepend('Oh yes!')) #在内部原有html代码前添加代码d = pq('<div class="wrap"><div id="test"><a href="http://cuiqingcai.com">Germy</a></div></div>') #创建一个dom树td = d('#test') #获取id为test的元素p.prependTo(td) #将p元素添加到td元素内,在td内部html代码的前面,源节点不变print(d)d.empty() #清空元素内部html代码print(d)print(u'=====================遍历====================')doc = pq(filename='test.html')lis = doc('li')for li in lis.items(): print(li.html()) #打印li元素的内部html代码print(lis.each(lambda e: e)) #each遍历函数,lambda表达式,不常用print(u'=====================网页请求====================')print(pq('http://www.525heart.com/index/index/index.html', headers={'user-agent': 'pyquery'})) #get请求方式,可设置headersprint(pq('http://httpbin.org/post', {'foo': 'bar'}, method='post', verify=True)) #post请求方式,可设置data和headers,已经控制是否检验
阅读全文
1 0
- python网络爬虫系列教程——python中pyquery库应用全解
- python网络爬虫系列教程——python中BeautifulSoup4库应用全解
- python网络爬虫系列教程——python中requests库应用全解
- python网络爬虫系列教程——python中urllib、urllib2、cookie模块应用全解
- python网络爬虫系列教程——python中lxml库应用全解(xpath表达式)
- python网络爬虫系列教程——PhantomJS包应用全解
- python网络爬虫系列教程——Scrapy框架应用全解
- python网络爬虫系列教程——Python+PhantomJS +Selenium组合应用
- 【Python】网络爬虫(一):pyquery一瞥
- python网络爬虫系列教程——python网络数据爬虫误区,让你的爬虫更像人类
- python应用系列教程——python的GUI界面编程Tkinter全解
- Python--python爬虫神器PyQuery
- 基于Python、PyQuery实现的一个网络爬虫实例
- Python 爬虫系列教程
- Python爬虫系列教程
- python数据分析系列教程——NumPy全解
- python数据分析系列教程——Pandas全解
- Python中PyQuery库的使用总结
- 数据结构实验之二叉树七:叶子问题
- 在TTF字体中提取指定文字
- SharedPreferences基本操作
- 数据库-关于丢失更新和乐观锁的那些故事
- HDU 2041 超级楼梯
- python网络爬虫系列教程——python中pyquery库应用全解
- ssh框架整合。
- ES6语法
- Java8 Collectors其它一些方法的使用
- gitblit git SERVER window 安装配置 hook post-receive 自动部署
- centos7 mysql数据库安装和配置 一、系统环境 yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release Cent
- JavaEE——Tomcat下载和启动
- Java泛型中E、T、K、V等的含义
- Fiddler的使用