目录
安装
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>