Python3网络爬虫开发实践——第4章—解析库的使用—4.3使用pyquery
2019-01-29 10:00
549 查看
1.准备工作
请先确保已经安装了pyquery库,使用import语句导入模块,如果导入失败,请先安装:
[code]pip install pyquery -i https://pypi.tuna.tsinghua.edu.cn/simple
导入模块:
[code]import pyquery
2.初始化
2.1 字符串初始化
[code]html = """ <div> <ul> <li class="item-O">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> """ doc = pyquery.PyQuery(html) print(doc("li"))
输出结果:
[code]<li class="item-O">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>
如上结果所示:输出了html中的所有li节点。
2.2 URL初始化
初始化的参数可以是字符串,也可以传入网页的URL
[code]doc = pyquery.PyQuery(url="https://cuiqingcai.com") print(doc("title"))
输出结果:
[code]<title>静觅丨崔庆才的个人博客</title>
2.3 文件初始化
[code]doc = pyquery.PyQuery(filename="demo.html") print(doc("li"))
这里需要一个本地文件:demo.html。上述代码首先会读取本地的文件内容,然后将文件内容以字符串的形式传递给Pyquery类进行初始化。
3.基本CSS选择器
相关文章推荐
- python解析html开发库pyquery使用方法
- Android开发使用Dom从网络端解析xml文件
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
- Python爬虫辅助利器PyQuery模块的安装使用攻略
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.3)源码及解析(简化更新)
- Python使用Mechanize模块编写爬虫的要点解析
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.2)源码及解析
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析(转)
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.3)源码及解析(简化更新)
- 【使用JSOUP实现网络爬虫】解析一个body片断
- python3使用urllib模块制作网络爬虫
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
- Python爬虫之使用BeautifulSoup解析HTML文本
- iOS开发使用JSON解析网络数据
- [Python]网络爬虫:糗事百科的网络爬虫(v0.2)源码及解析
- 使用python2.7学习网络爬虫,问题锦集
- Linux企业级项目实践之网络爬虫(16)——使用base64传输二进制数据
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析