码迷,mamicode.com
首页 > 编程语言 > 详细

数据之路 - Python爬虫 - PyQuery库

时间:2019-08-03 12:43:08      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:标签   port   rap   lin   解析   attr   rom   child   选择器   

一、什么是PyQuery?

PyQuery库也是一个非常强大又灵活的网页解析库。

官网地址:http://pyquery.readthedocs.io/en/latest/

 二、PyQuery基本库使用

html = ‘‘‘
<div> 
  <ul> 
    <li class="item-0">first item<lli> 
    <li class="item-1"><a href="link2.html">second item</a><lli> 
    <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>
‘‘‘

1.初始化

# 字符串初始化
from pyquery import PyQuery as pq
html = ""
doc = pd(html)
print(doc(li))

# URL初始化
from pyquery import PyQuery as pq
html = ""
doc = pq(url= https://cuiqingcai.com’) 
print(doc(’title))

# 文件初始化
from  pyquery import  PyQuery as pq
html = ""
doc = pq(filename=’demo.html’) 
print(doc(’li’))  

2.CSS选择器-获取标签

from pyquery import PyQuery as pq
doc = pd(html)

# 子元素
items = doc(.list)
lis = items.find(li)

lis = items.children()
lis = items.children(.active)
print(lis)

# 父元素
items = doc(.list)
container =items.parents()
print(container)

parent = items.parents(.wrap)
print(parent)

# 兄弟元素
li = doc(.list.item-0.active)
print(li.siblings())
print(li.siblings(.active))

3.CSS选择器-获取属性

from pyquery import PyQuery as pq
doc = pd(html)
a = doc(.item-0.active a)
print(a)
print(a.attr.href)
print(a.attr(href)

4.获取内容

from pyquery import PyQuery as pq
doc = pd(html)
a = doc(.item-0.active a)
print(a)
print(a.text())

5.获取HTML

from pyquery import PyQuery as pq
doc = pd(html)
li = doc(.item-0.active)
print(li)
print(li.html())

数据之路 - Python爬虫 - PyQuery库

标签:标签   port   rap   lin   解析   attr   rom   child   选择器   

原文地址:https://www.cnblogs.com/Iceredtea/p/11294266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!