首页 > 编程 > Python > 正文

Python网络爬虫神器PyQuery的基本使用教程

2020-02-22 23:07:34
字体:
来源:转载
供稿:网友

前言

pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好,和它差不多的还有BeautifulSoup,都是用来解析的。相比BeautifulSoup完美翔实的文档,虽然PyQuery库的文档弱爆了, 但是使用起来还是可以的,有些地方用起来很方便简洁。

安装

关于PyQuery的安装可以参考这篇文章://www.jb51.net/article/82955.htm

PyQuery库官方文档

初始化为PyQuery对象 常用的CCS选择器 伪类选择器 查找标签 获取标签信息

初始化为PyQuery对象

html = """<html lang="en"> <head> 简单好用的 <title>PyQuery</title> </head> <body> <ul id="container">  <li class="object-1">Python</li>  <li class="object-2">大法</li>  <li class="object-3">好</li> </ul> </body></html>"""

相当于BeautifulSoup库的初识化方法,将html转化为BeautifulSoup对象。

bsObj = BeautifulSoup(html, 'html.parser')

PyQuery库也要有自己的初始化。

1.1 将字符串初始化

from pyquery import PyQuery as pq#初始化为PyQuery对象doc = pq(html)print(type(doc))print(doc)

返回

<class 'pyquery.pyquery.PyQuery'><html lang="en"> <head> <title>PyQuery学习</title> </head> <body> <ul id="container">  <li class="object-1"/>  <li class="object-2"/>  <li class="object-3"/> </ul> </body></html>

1.2 将html文件初始化

#filename参数为html文件路径test_html = pq(filename = 'test.html')print(type(test_html))print(test_html)

返回

<class 'pyquery.pyquery.PyQuery'><html lang="en"> <head> <title>PyQuery学习</title> </head> <body> <ul id="container">  <li class="object-1"/>  <li class="object-2"/>  <li class="object-3"/> </ul> </body></html>

1.3 对网址响应进行初始化

response = pq(url = 'https://www.baidu.com')print(type(response))print(response)

返回

<class 'pyquery.pyquery.PyQuery'><html> <head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><meta content="always" name="referrer"/><link rel="stylesheet" type="text/css" href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="external nofollow" /><title>ç�¾åº¦ä¸�ä¸�ï¼�ä½ å°±ç�¥é��</title></head> <body link="#0000cc"> <div id="wrapper"> <div id="head"> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id="lg"> <img hidefocus="true" src="https://www.baidu.com/img/bd_logo1.png" width="270" height="129"/> </div> <form id="form" name="f" action="//www.baidu.com/s" class="fm"> <input type="hidden" name="bdorz_come" value="1"/> <input type="hidden" name="ie" value="utf-8"/> <input type="hidden" name="f" value="8"/> <input type="hidden" name="rsv_bp" value="1"/> <input type="hidden" name="rsv_idx" value="1"/> <input type="hidden" name="tn" value="baidu"/><span class="bg s_ipt_wr"><input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off" autofocus="autofocus"/></span><span class="bg s_btn_wr"><input type="submit" id="su" value="ç�¾åº¦ä¸�ä¸�" class="bg s_btn" autofocus=""/></span> </form> </div> </div> <div id="u1"> <a href="http://news.baidu.com" rel="external nofollow" name="tj_trnews" class="mnav">æ�°é�»</a> <a href="https://www.hao123.com" rel="external nofollow" name="tj_trhao123" class="mnav">hao123</a> <a href="http://map.baidu.com" rel="external nofollow" name="tj_trmap" class="mnav">å�°å�¾</a> <a href="http://v.baidu.com" rel="external nofollow" name="tj_trvideo" class="mnav">è§�é¢�</a> <a href="http://tieba.baidu.com" rel="external nofollow" name="tj_trtieba" class="mnav">è´´å�§</a> <noscript> <a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" rel="external nofollow" name="tj_login" class="lb">ç�»å½�</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç�»å½�</a>');
</script> <a href="//www.baidu.com/more/" rel="external nofollow" name="tj_briicon" class="bri" style="display: block;">æ�´å¤�产å��</a> </div> </div> </div> <div id="ftCon"> <div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com" rel="external nofollow" >å³äº�ç�¾åº¦</a> <a href="http://ir.baidu.com" rel="external nofollow" >About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/" rel="external nofollow" >使ç�¨ç�¾åº¦å��å¿è¯»</a> <a href="http://jianyi.baidu.com/" rel="external nofollow" class="cp-feedback">æ��è§�å��é¦�</a> 京ICPè¯�030173å�· <img src="https://www.baidu.com/img/gs.gif"/> </p> </div> </div> </div> </body> </html>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表