国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > 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"  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="http://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  rel="external nofollow" name="tj_trnews" class="mnav">æ�°é�»</a> <a  rel="external nofollow" name="tj_trhao123" class="mnav">hao123</a> <a  rel="external nofollow" name="tj_trmap" class="mnav">å�°å�¾</a> <a  rel="external nofollow" name="tj_trvideo" class="mnav">è§�é¢�</a> <a  rel="external nofollow" name="tj_trtieba" class="mnav">è´´å�§</a> <noscript> <a  rel="external nofollow" name="tj_login" class="lb">ç�»å½�</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç�»å½�</a>');
</script> <a  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  rel="external nofollow" >å³äº�ç�¾åº¦</a> <a  rel="external nofollow" >About Baidu</a> </p> <p id="cp">©2017 Baidu <a  rel="external nofollow" >使ç�¨ç�¾åº¦å��å¿è¯»</a> <a  rel="external nofollow" class="cp-feedback">æ��è§�å��é¦�</a> 京ICPè¯�030173å�· <img src="https://www.baidu.com/img/gs.gif"/> </p> </div> </div> </div> </body> </html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 侯马市| 米脂县| 平顺县| 临猗县| 化隆| 枝江市| 务川| 武邑县| 禄劝| 乳山市| 延安市| 金沙县| 怀仁县| 云龙县| 哈密市| 台湾省| 江山市| 汽车| 杭州市| 合阳县| 泾阳县| 布拖县| 桃江县| 息烽县| 文安县| 新龙县| 灵璧县| 改则县| 华阴市| 清水县| 花莲市| 宁陵县| 昭觉县| 祥云县| 无棣县| 高邮市| 呼和浩特市| 大渡口区| 巢湖市| 民丰县| 昌江|