目錄
安裝
pip install pyquery
使用
初始化
字符串初始化
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
# print(doc('div .cover'))
print(type(doc('div .cover')))
<class 'pyquery.pyquery.PyQuery'>
url初始化
from pyquery import PyQuery as pq
doc=pq(url="https://baidu.com")
print(doc("head"))
<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="http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css"/><title>ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“</title></head>
文件初始化
from pyquery import PyQuery as pq
filename= open('book_html.html','r', encoding='UTF-8')
doc=pq(filename='book_html.html')
print( doc('li').text())
with open ('book_html.html','r', encoding='UTF-8') as f:
print(f.read())
first item second item third item fourth item fifth item
<div>
<ul>
<li class="item-0">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>
基本CSS選擇器
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover a')) #class是cover的a標籤,並不一定是父子關係
<a href="https://book.douban.com/subject/30187217/?icn=index-editionrecommend" title="宋徽宗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29855169.jpg" class="" width="115px" height="172px" alt="宋徽宗"/>
</a>
<a href="https://book.douban.com/subject/27191009/?icn=index-editionrecommend" title="往事与随想">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834967.jpg" class="" width="115px" height="172px" alt="往事與隨想"/>
</a>
<a href="https://book.douban.com/subject/30289880/?icn=index-editionrecommend" title="生而不凡">
<img src="https://img3.doubanio.com/view/subject/m/public/s29861551.jpg" class="" width="115px" height="172px" alt="生而不凡"/>
</a>
<a href="https://book.douban.com/subject/30288357/?icn=index-editionrecommend" title="用一朵花改变世界">
<img src="https://img1.doubanio.com/view/subject/m/public/s29861548.jpg" class="" width="115px" height="172px" alt="用一朵花改變世界"/>
</a>
<a href="https://book.douban.com/subject/30143251/?icn=index-editionrecommend" title="老后破产">
<img src="https://img1.doubanio.com/view/subject/m/public/s29828367.jpg" class="" width="115px" height="172px" alt="老後破產"/>
</a>
<a href="https://book.douban.com/subject/30231603/?icn=index-latestbook-subject" title="火鸟">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834579.jpg" class="" width="115px" height="172px" alt="火鳥"/>
</a>
<a href="https://book.douban.com/subject/30265800/?icn=index-latestbook-subject" title="平常的恶">
<img src="https://img3.doubanio.com/view/subject/m/public/s29822801.jpg" class="" width="115px" height="172px" alt="平常的惡"/>
</a>
<a href="https://book.douban.com/subject/27605279/?icn=index-latestbook-subject" title="花街往事">
<img src="https://img3.doubanio.com/view/subject/m/public/s29858035.jpg" class="" width="115px" height="172px" alt="花街往事"/>
</a>
<a href="https://book.douban.com/subject/30186003/?icn=index-latestbook-subject" title="左道">
<img src="https://img3.doubanio.com/view/subject/m/public/s29860591.jpg" class="" width="115px" height="172px" alt="左道"/>
</a>
<a href="https://book.douban.com/subject/30270979/?icn=index-latestbook-subject" title="斜眼狗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29827989.jpg" class="" width="115px" height="172px" alt="斜眼狗"/>
</a>
<a href="https://book.douban.com/subject/30237192/?icn=index-latestbook-subject" title="莫迪里阿尼">
<img src="https://img3.doubanio.com/view/subject/m/public/s29813990.jpg" class="" width="115px" height="172px" alt="莫迪裏阿尼"/>
</a>
<a href="https://book.douban.com/subject/28356342/?icn=index-latestbook-subject" title="异类婚姻谭">
<img src="https://img1.doubanio.com/view/subject/m/public/s29842397.jpg" class="" width="115px" height="172px" alt="異類婚姻譚"/>
</a>
<a href="https://book.douban.com/subject/30262775/?icn=index-latestbook-subject" title="春之祭">
<img src="https://img3.doubanio.com/view/subject/m/public/s29857780.jpg" class="" width="115px" height="172px" alt="春之祭"/>
</a>
<a href="https://book.douban.com/subject/30255671/?icn=index-latestbook-subject" title="碗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29860149.jpg" class="" width="115px" height="172px" alt="碗"/>
</a>
<a href="https://book.douban.com/subject/27601590/?icn=index-latestbook-subject" title="姜饼人">
<img src="https://img3.doubanio.com/view/subject/m/public/s29849376.jpg" class="" width="115px" height="172px" alt="薑餅人"/>
</a>
<a href="https://book.douban.com/subject/27911231/?icn=index-latestbook-subject" title="万物并作">
<img src="https://img3.doubanio.com/view/subject/m/public/s29859121.jpg" class="" width="115px" height="172px" alt="萬物並作"/>
</a>
<a href="https://book.douban.com/subject/30294068/?icn=index-latestbook-subject" title="总觉得有哪里不错">
<img src="https://img3.doubanio.com/view/subject/m/public/s29862863.jpg" class="" width="115px" height="172px" alt="總覺得有哪裏不錯"/>
</a>
<a href="https://book.douban.com/subject/30240081/?icn=index-latestbook-subject" title="印度佛教史">
<img src="https://img1.doubanio.com/view/subject/m/public/s29857359.jpg" class="" width="115px" height="172px" alt="印度佛教史"/>
</a>
<a href="https://book.douban.com/subject/30275106/?icn=index-latestbook-subject" title="银河边缘001:奇境">
<img src="https://img1.doubanio.com/view/subject/m/public/s29845837.jpg" class="" width="115px" height="172px" alt="銀河邊緣001:奇境"/>
</a>
<a href="https://book.douban.com/subject/30272408/?icn=index-latestbook-subject" title="夏与西伯利亚">
<img src="https://img3.doubanio.com/view/subject/m/public/s29817584.jpg" class="" width="115px" height="172px" alt="夏與西伯利亞"/>
</a>
<a href="https://book.douban.com/subject/27622645/?icn=index-latestbook-subject" title="崖山">
<img src="https://img1.doubanio.com/view/subject/m/public/s29827419.jpg" class="" width="115px" height="172px" alt="崖山"/>
</a>
<a href="https://book.douban.com/subject/30300327/?icn=index-latestbook-subject" title="料理图鉴">
<img src="https://img3.doubanio.com/view/subject/m/public/s29845861.jpg" class="" width="115px" height="172px" alt="料理圖鑑"/>
</a>
<a href="https://book.douban.com/subject/30247619/?icn=index-latestbook-subject" title="坚果壳">
<img src="https://img3.doubanio.com/view/subject/m/public/s29840586.jpg" class="" width="115px" height="172px" alt="堅果殼"/>
</a>
<a href="https://book.douban.com/subject/30259150/?icn=index-latestbook-subject" title="失明的摄影师">
<img src="https://img3.doubanio.com/view/subject/m/public/s29857786.jpg" class="" width="115px" height="172px" alt="失明的攝影師"/>
</a>
<a href="https://book.douban.com/subject/30245839/?icn=index-latestbook-subject" title="华龙之宫">
<img src="https://img1.doubanio.com/view/subject/m/public/s29822379.jpg" class="" width="115px" height="172px" alt="華龍之宮"/>
</a>
<a href="https://book.douban.com/subject/30273572/?icn=index-latestbook-subject" title="识字的用途">
<img src="https://img3.doubanio.com/view/subject/m/public/s29826400.jpg" class="" width="115px" height="172px" alt="識字的用途"/>
</a>
<a href="https://book.douban.com/subject/30282245/?icn=index-latestbook-subject" title="盂兰变">
<img src="https://img3.doubanio.com/view/subject/m/public/s29828661.jpg" class="" width="115px" height="172px" alt="盂蘭變"/>
</a>
<a href="https://book.douban.com/subject/30289118/?icn=index-latestbook-subject" title="未完的明治维新">
<img src="https://img3.doubanio.com/view/subject/m/public/s29851351.jpg" class="" width="115px" height="172px" alt="未完的明治維新"/>
</a>
<a href="https://book.douban.com/subject/30276928/?icn=index-latestbook-subject" title="亲爱的陌生人">
<img src="https://img3.doubanio.com/view/subject/m/public/s29821945.jpg" class="" width="115px" height="172px" alt="親愛的陌生人"/>
</a>
<a href="https://book.douban.com/subject/30246798/?icn=index-latestbook-subject" title="旅行手帐完全指南">
<img src="https://img3.doubanio.com/view/subject/m/public/s29792026.jpg" class="" width="115px" height="172px" alt="旅行手帳完全指南"/>
</a>
<a href="https://book.douban.com/subject/26952667/?icn=index-latestbook-subject" title="隐剑孤影抄">
<img src="https://img3.doubanio.com/view/subject/m/public/s29831804.jpg" class="" width="115px" height="172px" alt="隱劍孤影抄"/>
</a>
<a href="https://book.douban.com/subject/30289362/?icn=index-latestbook-subject" title="明年更年轻:运动赋能篇">
<img src="https://img3.doubanio.com/view/subject/m/public/s29834582.jpg" class="" width="115px" height="172px" alt="明年更年輕:運動賦能篇"/>
</a>
<a href="https://book.douban.com/subject/30262577/?icn=index-latestbook-subject" title="天使之触">
<img src="https://img1.doubanio.com/view/subject/m/public/s29815629.jpg" class="" width="115px" height="172px" alt="天使之觸"/>
</a>
<a href="https://book.douban.com/subject/30237881/?icn=index-latestbook-subject" title="独自迈向生命的尽头">
<img src="https://img3.doubanio.com/view/subject/m/public/s29804306.jpg" class="" width="115px" height="172px" alt="獨自邁向生命的盡頭"/>
</a>
<a href="https://book.douban.com/subject/30237215/?icn=index-latestbook-subject" title="时间不存在">
<img src="https://img3.doubanio.com/view/subject/m/public/s29860382.jpg" class="" width="115px" height="172px" alt="時間不存在"/>
</a>
<a href="https://book.douban.com/subject/30276254/?icn=index-latestbook-subject" title="正午6">
<img src="https://img3.doubanio.com/view/subject/m/public/s29834023.jpg" class="" width="115px" height="172px" alt="正午6"/>
</a>
<a href="https://book.douban.com/subject/30241252/?icn=index-latestbook-subject" title="弗兰肯斯坦在巴格达">
<img src="https://img1.doubanio.com/view/subject/m/public/s29819087.jpg" class="" width="115px" height="172px" alt="弗蘭肯斯坦在巴格達"/>
</a>
<a href="https://book.douban.com/subject/30240074/?icn=index-latestbook-subject" title="我每天只工作3小时">
<img src="https://img3.doubanio.com/view/subject/m/public/s29846566.jpg" class="" width="115px" height="172px" alt="我每天只工作3小時"/>
</a>
<a href="https://book.douban.com/subject/30243868/?icn=index-latestbook-subject" title="犹太警察工会">
<img src="https://img3.doubanio.com/view/subject/m/public/s29796735.jpg" class="" width="115px" height="172px" alt="猶太警察工會"/>
</a>
<a href="https://book.douban.com/subject/30243056/?icn=index-latestbook-subject" title="木匠手记">
<img src="https://img1.doubanio.com/view/subject/m/public/s29788169.jpg" class="" width="115px" height="172px" alt="木匠手記"/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30224833/?icn=index-topchart-subject">
<img src="https://img1.doubanio.com/view/subject/m/public/s29845937.jpg" alt="聖殿春秋" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30183051/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29767895.jpg" alt="魚翅與花椒" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30163282/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29816545.jpg" alt="西西里檸檬" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30259720/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29827660.jpg" alt="今日簡史" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30224187/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29810441.jpg" alt="俠隱" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/27191009/?icn=index-topchart-subject">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834967.jpg" alt="往事與隨想" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/27194592/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29796113.jpg" alt="雙峯:最終檔案" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/27091742/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29663440.jpg" alt="鄰人之妻" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30203737/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29793036.jpg" alt="韃靼人沙漠" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30199056/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29765205.jpg" alt="鐵道之旅" class=""/>
</a>
<a href="https://market.douban.com/book/zen?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web">
<div class="pic" style="background-image: url(https://img3.doubanio.com/view/freyr_page_photo/raw/public/3063.jpg)"/>
</a>
<a href="https://market.douban.com/book/huoniao?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/3035.jpg" width="106" height="140" alt="火鳥"/>
</a>
<a href="https://market.douban.com/book/shuangfeng2?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2942.jpg" width="106" height="140" alt="雙峯:最終檔案"/>
</a>
<a href="https://market.douban.com/book/birds?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2883.jpg" width="106" height="140" alt="世界上的鳥兒"/>
</a>
<a href="https://market.douban.com/book/qishi?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2482.jpg" width="106" height="140" alt="刺殺騎士團長"/>
</a>
<a href="https://market.douban.com/book/jinglingbiaoben?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img1.doubanio.com/view/freyr_page_photo/raw/public/2727.jpg" width="106" height="140" alt="精靈標本"/>
</a>
查找元素
查找子元素
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').find('a')) #class是cover的標籤下a標籤,是父子關係
<a href="https://book.douban.com/subject/30187217/?icn=index-editionrecommend" title="宋徽宗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29855169.jpg" class="" width="115px" height="172px" alt="宋徽宗"/>
</a>
<a href="https://book.douban.com/subject/27191009/?icn=index-editionrecommend" title="往事与随想">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834967.jpg" class="" width="115px" height="172px" alt="往事與隨想"/>
</a>
<a href="https://book.douban.com/subject/30289880/?icn=index-editionrecommend" title="生而不凡">
<img src="https://img3.doubanio.com/view/subject/m/public/s29861551.jpg" class="" width="115px" height="172px" alt="生而不凡"/>
</a>
<a href="https://book.douban.com/subject/30288357/?icn=index-editionrecommend" title="用一朵花改变世界">
<img src="https://img1.doubanio.com/view/subject/m/public/s29861548.jpg" class="" width="115px" height="172px" alt="用一朵花改變世界"/>
</a>
<a href="https://book.douban.com/subject/30143251/?icn=index-editionrecommend" title="老后破产">
<img src="https://img1.doubanio.com/view/subject/m/public/s29828367.jpg" class="" width="115px" height="172px" alt="老後破產"/>
</a>
<a href="https://book.douban.com/subject/30281971/?icn=index-latestbook-subject" title="推理作家的信条">
<img src="https://img3.doubanio.com/view/subject/m/public/s29829351.jpg" class="" width="115px" height="172px" alt="推理作家的信條"/>
</a>
<a href="https://book.douban.com/subject/30289118/?icn=index-latestbook-subject" title="未完的明治维新">
<img src="https://img3.doubanio.com/view/subject/m/public/s29851351.jpg" class="" width="115px" height="172px" alt="未完的明治維新"/>
</a>
<a href="https://book.douban.com/subject/27601590/?icn=index-latestbook-subject" title="姜饼人">
<img src="https://img3.doubanio.com/view/subject/m/public/s29849376.jpg" class="" width="115px" height="172px" alt="薑餅人"/>
</a>
<a href="https://book.douban.com/subject/30272408/?icn=index-latestbook-subject" title="夏与西伯利亚">
<img src="https://img3.doubanio.com/view/subject/m/public/s29817584.jpg" class="" width="115px" height="172px" alt="夏與西伯利亞"/>
</a>
<a href="https://book.douban.com/subject/30247619/?icn=index-latestbook-subject" title="坚果壳">
<img src="https://img3.doubanio.com/view/subject/m/public/s29840586.jpg" class="" width="115px" height="172px" alt="堅果殼"/>
</a>
<a href="https://book.douban.com/subject/30240081/?icn=index-latestbook-subject" title="印度佛教史">
<img src="https://img1.doubanio.com/view/subject/m/public/s29857359.jpg" class="" width="115px" height="172px" alt="印度佛教史"/>
</a>
<a href="https://book.douban.com/subject/30231603/?icn=index-latestbook-subject" title="火鸟">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834579.jpg" class="" width="115px" height="172px" alt="火鳥"/>
</a>
<a href="https://book.douban.com/subject/30265800/?icn=index-latestbook-subject" title="平常的恶">
<img src="https://img3.doubanio.com/view/subject/m/public/s29822801.jpg" class="" width="115px" height="172px" alt="平常的惡"/>
</a>
<a href="https://book.douban.com/subject/28356342/?icn=index-latestbook-subject" title="异类婚姻谭">
<img src="https://img1.doubanio.com/view/subject/m/public/s29842397.jpg" class="" width="115px" height="172px" alt="異類婚姻譚"/>
</a>
<a href="https://book.douban.com/subject/30203779/?icn=index-latestbook-subject" title="空王冠">
<img src="https://img3.doubanio.com/view/subject/m/public/s29861650.jpg" class="" width="115px" height="172px" alt="空王冠"/>
</a>
<a href="https://book.douban.com/subject/30282245/?icn=index-latestbook-subject" title="盂兰变">
<img src="https://img3.doubanio.com/view/subject/m/public/s29828661.jpg" class="" width="115px" height="172px" alt="盂蘭變"/>
</a>
<a href="https://book.douban.com/subject/30186003/?icn=index-latestbook-subject" title="左道">
<img src="https://img3.doubanio.com/view/subject/m/public/s29860591.jpg" class="" width="115px" height="172px" alt="左道"/>
</a>
<a href="https://book.douban.com/subject/30294068/?icn=index-latestbook-subject" title="总觉得有哪里不错">
<img src="https://img3.doubanio.com/view/subject/m/public/s29862863.jpg" class="" width="115px" height="172px" alt="總覺得有哪裏不錯"/>
</a>
<a href="https://book.douban.com/subject/30237881/?icn=index-latestbook-subject" title="独自迈向生命的尽头">
<img src="https://img3.doubanio.com/view/subject/m/public/s29804306.jpg" class="" width="115px" height="172px" alt="獨自邁向生命的盡頭"/>
</a>
<a href="https://book.douban.com/subject/30243868/?icn=index-latestbook-subject" title="犹太警察工会">
<img src="https://img3.doubanio.com/view/subject/m/public/s29796735.jpg" class="" width="115px" height="172px" alt="猶太警察工會"/>
</a>
<a href="https://book.douban.com/subject/30275106/?icn=index-latestbook-subject" title="银河边缘001:奇境">
<img src="https://img1.doubanio.com/view/subject/m/public/s29845837.jpg" class="" width="115px" height="172px" alt="銀河邊緣001:奇境"/>
</a>
<a href="https://book.douban.com/subject/30273572/?icn=index-latestbook-subject" title="识字的用途">
<img src="https://img3.doubanio.com/view/subject/m/public/s29826400.jpg" class="" width="115px" height="172px" alt="識字的用途"/>
</a>
<a href="https://book.douban.com/subject/30241252/?icn=index-latestbook-subject" title="弗兰肯斯坦在巴格达">
<img src="https://img1.doubanio.com/view/subject/m/public/s29819087.jpg" class="" width="115px" height="172px" alt="弗蘭肯斯坦在巴格達"/>
</a>
<a href="https://book.douban.com/subject/30246798/?icn=index-latestbook-subject" title="旅行手帐完全指南">
<img src="https://img3.doubanio.com/view/subject/m/public/s29792026.jpg" class="" width="115px" height="172px" alt="旅行手帳完全指南"/>
</a>
<a href="https://book.douban.com/subject/27605279/?icn=index-latestbook-subject" title="花街往事">
<img src="https://img3.doubanio.com/view/subject/m/public/s29858035.jpg" class="" width="115px" height="172px" alt="花街往事"/>
</a>
<a href="https://book.douban.com/subject/30237192/?icn=index-latestbook-subject" title="莫迪里阿尼">
<img src="https://img3.doubanio.com/view/subject/m/public/s29813990.jpg" class="" width="115px" height="172px" alt="莫迪裏阿尼"/>
</a>
<a href="https://book.douban.com/subject/27127568/?icn=index-latestbook-subject" title="春天">
<img src="https://img3.doubanio.com/view/subject/m/public/s29802803.jpg" class="" width="115px" height="172px" alt="春天"/>
</a>
<a href="https://book.douban.com/subject/30259150/?icn=index-latestbook-subject" title="失明的摄影师">
<img src="https://img3.doubanio.com/view/subject/m/public/s29857786.jpg" class="" width="115px" height="172px" alt="失明的攝影師"/>
</a>
<a href="https://book.douban.com/subject/27622645/?icn=index-latestbook-subject" title="崖山">
<img src="https://img1.doubanio.com/view/subject/m/public/s29827419.jpg" class="" width="115px" height="172px" alt="崖山"/>
</a>
<a href="https://book.douban.com/subject/30254388/?icn=index-latestbook-subject" title="两个世界的战争">
<img src="https://img3.doubanio.com/view/subject/m/public/s29841535.jpg" class="" width="115px" height="172px" alt="兩個世界的戰爭"/>
</a>
<a href="https://book.douban.com/subject/30276928/?icn=index-latestbook-subject" title="亲爱的陌生人">
<img src="https://img3.doubanio.com/view/subject/m/public/s29821945.jpg" class="" width="115px" height="172px" alt="親愛的陌生人"/>
</a>
<a href="https://book.douban.com/subject/27911231/?icn=index-latestbook-subject" title="万物并作">
<img src="https://img3.doubanio.com/view/subject/m/public/s29859121.jpg" class="" width="115px" height="172px" alt="萬物並作"/>
</a>
<a href="https://book.douban.com/subject/30255671/?icn=index-latestbook-subject" title="碗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29860149.jpg" class="" width="115px" height="172px" alt="碗"/>
</a>
<a href="https://book.douban.com/subject/30240074/?icn=index-latestbook-subject" title="我每天只工作3小时">
<img src="https://img3.doubanio.com/view/subject/m/public/s29846566.jpg" class="" width="115px" height="172px" alt="我每天只工作3小時"/>
</a>
<a href="https://book.douban.com/subject/30270979/?icn=index-latestbook-subject" title="斜眼狗">
<img src="https://img1.doubanio.com/view/subject/m/public/s29827989.jpg" class="" width="115px" height="172px" alt="斜眼狗"/>
</a>
<a href="https://book.douban.com/subject/30300327/?icn=index-latestbook-subject" title="料理图鉴">
<img src="https://img3.doubanio.com/view/subject/m/public/s29845861.jpg" class="" width="115px" height="172px" alt="料理圖鑑"/>
</a>
<a href="https://book.douban.com/subject/30245839/?icn=index-latestbook-subject" title="华龙之宫">
<img src="https://img1.doubanio.com/view/subject/m/public/s29822379.jpg" class="" width="115px" height="172px" alt="華龍之宮"/>
</a>
<a href="https://book.douban.com/subject/30243056/?icn=index-latestbook-subject" title="木匠手记">
<img src="https://img1.doubanio.com/view/subject/m/public/s29788169.jpg" class="" width="115px" height="172px" alt="木匠手記"/>
</a>
<a href="https://book.douban.com/subject/26952667/?icn=index-latestbook-subject" title="隐剑孤影抄">
<img src="https://img3.doubanio.com/view/subject/m/public/s29831804.jpg" class="" width="115px" height="172px" alt="隱劍孤影抄"/>
</a>
<a href="https://book.douban.com/subject/30289362/?icn=index-latestbook-subject" title="明年更年轻:运动赋能篇">
<img src="https://img3.doubanio.com/view/subject/m/public/s29834582.jpg" class="" width="115px" height="172px" alt="明年更年輕:運動賦能篇"/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30224833/?icn=index-topchart-subject">
<img src="https://img1.doubanio.com/view/subject/m/public/s29845937.jpg" alt="聖殿春秋" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30183051/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29767895.jpg" alt="魚翅與花椒" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30163282/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29816545.jpg" alt="西西里檸檬" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30259720/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29827660.jpg" alt="今日簡史" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30224187/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29810441.jpg" alt="俠隱" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/27191009/?icn=index-topchart-subject">
<img src="https://img1.doubanio.com/view/subject/m/public/s29834967.jpg" alt="往事與隨想" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/27194592/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29796113.jpg" alt="雙峯:最終檔案" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/27091742/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29663440.jpg" alt="鄰人之妻" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_fiction'})" href="https://book.douban.com/subject/30203737/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29793036.jpg" alt="韃靼人沙漠" class=""/>
</a>
<a onclick="moreurl(this, {from:'pop_nonfiction'})" href="https://book.douban.com/subject/30199056/?icn=index-topchart-subject">
<img src="https://img3.doubanio.com/view/subject/m/public/s29765205.jpg" alt="鐵道之旅" class=""/>
</a>
<a href="https://market.douban.com/book/zen?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web">
<div class="pic" style="background-image: url(https://img3.doubanio.com/view/freyr_page_photo/raw/public/3063.jpg)"/>
</a>
<a href="https://market.douban.com/book/huoniao?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/3035.jpg" width="106" height="140" alt="火鳥"/>
</a>
<a href="https://market.douban.com/book/shuangfeng2?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2942.jpg" width="106" height="140" alt="雙峯:最終檔案"/>
</a>
<a href="https://market.douban.com/book/birds?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2883.jpg" width="106" height="140" alt="世界上的鳥兒"/>
</a>
<a href="https://market.douban.com/book/qishi?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img3.doubanio.com/view/freyr_page_photo/raw/public/2482.jpg" width="106" height="140" alt="刺殺騎士團長"/>
</a>
<a href="https://market.douban.com/book/jinglingbiaoben?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web" target="_blank">
<img src="https://img1.doubanio.com/view/freyr_page_photo/raw/public/2727.jpg" width="106" height="140" alt="精靈標本"/>
</a>
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').children()) #class是cover的標籤下子標籤標籤,是父子關係
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').children('a')) #class是cover的標籤下子標籤爲a的標籤,是父子關係
查找父元素
parent
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').parent()) #class是cover的標籤的父標籤,是父子關係
parents
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').parents()) #class是cover的標籤的所有祖先標籤,從最大的開始返回,也就是最外層標籤
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').parents('.slide-list')) #class是cover的標籤的class是slide-list的祖先標籤
查找兄弟元素
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
# print(doc('.cover').siblings()) #class是cover的標籤的所有兄弟標籤
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
print(doc('.cover').siblings('.intervenor-info')) #class是cover的標籤的class是intervenor-info的兄弟標籤
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
遍歷
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
covers=doc('.cover').items() #class是cover的標籤的class是intervenor-info的兄弟標籤
for cover in covers:
print(cover)
## 獲取屬性
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
a=doc('.cover a').items() #class是cover的標籤的a標籤
for item in a:
print(item.attr('href'),item.attr('title'))
https://book.douban.com/subject/30187217/?icn=index-editionrecommend 宋徽宗
https://book.douban.com/subject/27191009/?icn=index-editionrecommend 往事與隨想
https://book.douban.com/subject/30289880/?icn=index-editionrecommend 生而不凡
https://book.douban.com/subject/30288357/?icn=index-editionrecommend 用一朵花改變世界
https://book.douban.com/subject/30143251/?icn=index-editionrecommend 老後破產
https://book.douban.com/subject/30243868/?icn=index-latestbook-subject 猶太警察工會
https://book.douban.com/subject/30300327/?icn=index-latestbook-subject 料理圖鑑
https://book.douban.com/subject/30255671/?icn=index-latestbook-subject 碗
https://book.douban.com/subject/30240081/?icn=index-latestbook-subject 印度佛教史
https://book.douban.com/subject/30281971/?icn=index-latestbook-subject 推理作家的信條
https://book.douban.com/subject/30259150/?icn=index-latestbook-subject 失明的攝影師
https://book.douban.com/subject/30262577/?icn=index-latestbook-subject 天使之觸
https://book.douban.com/subject/30262775/?icn=index-latestbook-subject 春之祭
https://book.douban.com/subject/30237215/?icn=index-latestbook-subject 時間不存在
https://book.douban.com/subject/27911231/?icn=index-latestbook-subject 萬物並作
https://book.douban.com/subject/30241252/?icn=index-latestbook-subject 弗蘭肯斯坦在巴格達
https://book.douban.com/subject/30240074/?icn=index-latestbook-subject 我每天只工作3小時
https://book.douban.com/subject/30247619/?icn=index-latestbook-subject 堅果殼
https://book.douban.com/subject/30276254/?icn=index-latestbook-subject 正午6
https://book.douban.com/subject/30245839/?icn=index-latestbook-subject 華龍之宮
https://book.douban.com/subject/30186003/?icn=index-latestbook-subject 左道
https://book.douban.com/subject/27127568/?icn=index-latestbook-subject 春天
https://book.douban.com/subject/30243056/?icn=index-latestbook-subject 木匠手記
https://book.douban.com/subject/30270979/?icn=index-latestbook-subject 斜眼狗
https://book.douban.com/subject/30203779/?icn=index-latestbook-subject 空王冠
https://book.douban.com/subject/30231603/?icn=index-latestbook-subject 火鳥
https://book.douban.com/subject/30265800/?icn=index-latestbook-subject 平常的惡
https://book.douban.com/subject/28356342/?icn=index-latestbook-subject 異類婚姻譚
https://book.douban.com/subject/30282245/?icn=index-latestbook-subject 盂蘭變
https://book.douban.com/subject/30237192/?icn=index-latestbook-subject 莫迪裏阿尼
https://book.douban.com/subject/30294068/?icn=index-latestbook-subject 總覺得有哪裏不錯
https://book.douban.com/subject/30237881/?icn=index-latestbook-subject 獨自邁向生命的盡頭
https://book.douban.com/subject/30276928/?icn=index-latestbook-subject 親愛的陌生人
https://book.douban.com/subject/30289362/?icn=index-latestbook-subject 明年更年輕:運動賦能篇
https://book.douban.com/subject/27622645/?icn=index-latestbook-subject 崖山
https://book.douban.com/subject/30254388/?icn=index-latestbook-subject 兩個世界的戰爭
https://book.douban.com/subject/30275106/?icn=index-latestbook-subject 銀河邊緣001:奇境
https://book.douban.com/subject/30273572/?icn=index-latestbook-subject 識字的用途
https://book.douban.com/subject/27601590/?icn=index-latestbook-subject 薑餅人
https://book.douban.com/subject/30289118/?icn=index-latestbook-subject 未完的明治維新
https://book.douban.com/subject/30224833/?icn=index-topchart-subject None
https://book.douban.com/subject/30183051/?icn=index-topchart-subject None
https://book.douban.com/subject/30163282/?icn=index-topchart-subject None
https://book.douban.com/subject/30259720/?icn=index-topchart-subject None
https://book.douban.com/subject/30224187/?icn=index-topchart-subject None
https://book.douban.com/subject/27191009/?icn=index-topchart-subject None
https://book.douban.com/subject/27194592/?icn=index-topchart-subject None
https://book.douban.com/subject/27091742/?icn=index-topchart-subject None
https://book.douban.com/subject/30203737/?icn=index-topchart-subject None
https://book.douban.com/subject/30199056/?icn=index-topchart-subject None
https://market.douban.com/book/zen?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
https://market.douban.com/book/huoniao?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
https://market.douban.com/book/shuangfeng2?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
https://market.douban.com/book/birds?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
https://market.douban.com/book/qishi?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
https://market.douban.com/book/jinglingbiaoben?utm_campaign=book_freyr_section&utm_source=douban&utm_medium=pc_web None
獲取文本
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
a=doc('.intervenor-info span').items() #class是cover的標籤的a標籤
for item in a:
print(item.text())
推薦
推薦
推薦
推薦
推薦
獲取HTML
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
a=doc('.intervenor-info').items() #class是intervenor-info的標籤
for item in a:
print(item.html())
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
DOM操作
修改class
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
a=doc('.intervenor-info') #class是intervenor-info的標籤
print(a)
a.remove_class('intervenor-info') #把class=intervenor-info 移除
print(a)
a.add_class('intervenor-info') #把class=intervenor-info 添加回去
print(a)
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
修改屬性
import requests
from pyquery import PyQuery as pq
html = requests.get("https://book.douban.com").text #獲取html代碼
doc=pq(html)
a=doc('.intervenor-info') #class是intervenor-info的標籤
print(a)
a.attr('name','domain') #把name=domain屬性,無則添加,有則更改
print(a)
a.css('font-size','14px')# 添加font-size =14px的style
print(a)
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain" style="font-size: 14px">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain" style="font-size: 14px">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain" style="font-size: 14px">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain" style="font-size: 14px">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
<div class="intervenor-info" name="domain" style="font-size: 14px">
<img src="https://img3.doubanio.com/f/book/ef040178fab1770d60e3f2f12ba4c7aa70714396/pics/book/partner/jd_recommend.png" class="jd-icon" width="16" height="16"/>
<span>推薦</span>
</div>
remove
import requests
from pyquery import PyQuery as pq
html="""
<div class="cover">
<a href="https://book.douban.com/subject/30187217/?icn=index-editionrecommend" title="宋徽宗">
hello,domain!
<p>pppppp</p>
</a>
</div>
"""
doc=pq(html)
a=doc('.cover') #class是intervenor-info的標籤
print(a)
a.find('p').remove() #找到a標籤並且移除
print(a)
<div class="cover">
<a href="https://book.douban.com/subject/30187217/?icn=index-editionrecommend" title="宋徽宗">
hello,domain!
<p>pppppp</p>
</a>
</div>
<div class="cover">
<a href="https://book.douban.com/subject/30187217/?icn=index-editionrecommend" title="宋徽宗">
hello,domain!
</a>
</div>