本文实例讲述了python爬虫学习笔记之pyquery模块基本用法。分享给大家供大家参考,具体如下:
首发时间:2018-03-09 21:26
pip3 install pyquery
from pyquery import PyQuery as pq
【使用PyQuery初始化解析对象,PyQuery是一个类,直接将要解析的对象作为参数传入即可】
textParse = pq(html)
# urlParse = pq('http://www.baidu.com') #1
urlParse = pq(url='http://www.baidu.com') #2
fileParse = pq(filename="L:\demo.html")
result = textParse('h2').text()
result3=textParse(".p1").text()
result4=textParse("#user").attr("type")
result5=textParse("p,div").text()
result6=textParse("div a").attr.href
result7=textParse("[class='p1']").text()
result8=textParse("p:last").text()
(更多的,可以参考css)
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div>
123
<a id="a1" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
###初始化
textParse = pq(html)
# urlParse = pq('http://www.baidu.com') #1
# urlParse = pq(url='http://www.baidu.com') #2
# fileParse = pq(filename="L:\demo.html")
##获取
result = textParse('h2').text()
print(result)
result2= textParse('div').html()
print(result2)
result3=textParse(".p1").text()
print(result3)
result4=textParse("#user").attr("type")
print(result4)
result5=textParse("p,div").text()
print(result5)
result6=textParse("div a").attr.href
print(result6)
result7=textParse("[class='p1']").text()
print(result7)
result8=textParse("p:last").text()
print(result8)
result9=textParse("div").find("a").text()
print(result9)
result12=textParse("p").filter(".p1").text()
print(result12)
result10=textParse("div").children()
print(result10)
result11=textParse("a").parent()
print(result11)
attr(attribute):获取属性
result2=textParse("a").attr("href")
attr.xxxx:获取属性xxxx
result21=textParse("a").attr.href
result22=textParse("a").attr.class_
text():获取文本,子元素中也仅仅返回文本
result1=textParse("a").text()
html():获取html,功能与text类似,但返回html标签
result3=textParse("div").html()
补充1:
元素的迭代:如果返回的结果是多个元素,如果想迭代出每个元素,可以使用items():
补充2:pyquery是jquery的python化,语法基本都是相通的,想了解更多,可以参考jquery。
add_class():增加class
remove_class():移除class
remove():删除指定元素
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue">
123
<a class="ca" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
textParse=pq(html)
textParse('a').add_class("c1")
print(textParse('a').attr("class"))
textParse('a').remove_class("c1")
print(textParse('a').attr("class"))
print(textParse('div').html())
textParse('div').remove("a")
print(textParse('div').html())
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue">
123
<a class="ca" href="http://www.baidu.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
textParse=pq(html)
textParse('a').attr("name","hehe")
print(textParse('a').attr("name"))
textParse('a').css("color","white")
textParse('a').css({"background-color":"black","postion":"fixed"})
print(textParse('a').attr("style"))
这些操作什么时候会被用到:
【有时候可能会将数据样式处理一下再存储下来,就需要用到,比如我获取下来的数据样式我不满意,可以自定义成我自己的格式】
【有时候需要逐层清理再筛选出指定结果,比如
中,如果仅仅想要获取123就可以先删除再获取】先使用审查元素,定位目标元素
确认爬取信息
要注意的是,豆瓣新书是有一些分在后面页的,实际上目标应该是li的上一级ul:
使用PyQuery筛选出结果:
from pyquery import PyQuery as pq
urlParse=pq(url="https://book.douban.com/")
info=urlParse("div.carousel ul li div.info")
file=open("demo.txt","w",encoding="utf8")
for i in info.items():
title=i.find("div.title")
author=i.find("span.author")
abstract=i.find(".abstract")
file.write("标题:"+title.text()+"\n")
file.write("作者:"+author.text()+"\n")
file.write("概要:"+abstract.text()+"\n")
file.write("-----------------\n")
print("\n")
file.close()
希望本文所述对大家Python程序设计有所帮助。
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!