当前位置: 首页 > news >正文

python解析库 爬虫_python 爬虫(二)- 解析库 的简单使用

当我们在获取到网页相应内容的时候, 就会使用去解析它 过滤得到想要的内容

正则re

lxml 库

Beautiful Soup

pyquery

JsonPath

示例响应内容

http://quotes.toscrape.com/ 截取部分内容, 以下所有例子将以这个响应内容来示范, 假设响应的内容字符串 定义为一个变量 content

一、正则re

使用python 中内置的模块 re正则模块

如解析页面 上所有的名人的名字:

import re

pat = re.compile('(.*?)')

print(pat.findall(content))

输出:['Albert Einstein', 'J.K. Rowling', 'Albert Einstein', 'Jane Austen', 'Marilyn Monroe', 'Albert Einstein', 'André Gide', 'Thomas A. Edison', 'Eleanor Roosevelt', 'Steve Martin']

二、lxml库

lxml 支持xpath 的解析方式,那什么是xpath解析呢?

XPath 使用路径表达式来选取 XML 文档中的节点或节点集。节点是通过沿着路径 (path) 或者步 (steps) 来选取的。 xpath 解析方式

同样使用上面的例子,首先需要安装 lxml库

from lxml import etree

html = etree.HTML(content)

authors = html.xpath("//small[@class='author']//text()")

print(authors)

三、Beautiful Soup

BeautifulSoup也是Python的一个HTML或XML解析库,最主要的功能就是从网页爬取我们需要的数据。

首先需要安装 BeautifulSoup 解析器 pip install beautifulsoup4

from bs4 import BeautifulSoup

soup = BeautifulSoup(content,"lxml")

authors = soup.select('small.author')

for author in authors:

print(author.get_text())

四、pyquery

pyquery语法与前端 jQuery的用法几乎一样

from pyquery import PyQuery as pq

doc = pq(content)

authors = doc('small.author')

for author in authors.items():

print(author.text())

会使用jsonpath的地方 , 一般响应的内容 是json数据 。

语法:

XPath

JSONPath

Result

/store/book/author

$.store.book[*].author

the authors of all books in the store

//author

$..author

all authors

/store/*

$.store.*

all things in store, which are some books and a red bicycle.

/store//price

$.store..price

the price of everything in the store.

//book[3]

$..book[2]

the third book

//book[last()]

$..book[(@.length-1)] $..book[-1:]

the last book in order.

//book[position()<3]

$..book[0,1] $..book[:2]

the first two books

//book[isbn]

$..book[?(@.isbn)]

filter all books with isbn number

//book[price<10]

$..book[?(@.price<10)]

filter all books cheapier than 10

//*

$..*

all Elements in XML document. All members of JSON structure.

这里使用 一段 json 数据

我们来获取 所有的作者 和 所有价格

import jsonpath

import json

json_str = '''

{ "store": {

"book": [

{ "category": "reference",

"author": "Nigel Rees",

"title": "Sayings of the Century",

"price": 8.95

},

{ "category": "fiction",

"author": "Evelyn Waugh",

"title": "Sword of Honour",

"price": 12.99

},

{ "category": "fiction",

"author": "Herman Melville",

"title": "Moby Dick",

"isbn": "0-553-21311-3",

"price": 8.99

},

{ "category": "fiction",

"author": "J. R. R. Tolkien",

"title": "The Lord of the Rings",

"isbn": "0-395-19395-8",

"price": 22.99

}

],

"bicycle": {

"color": "red",

"price": 19.95

}

}

}

'''

jc = json.loads(json_str)

jp = jsonpath.jsonpath(jc, '$..author')

print(jp)

jp = jsonpath.jsonpath(jc, '$.store..price')

print(jp)

输出:

['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']

[8.95, 12.99, 8.99, 22.99, 19.95]

相关文章:

  • 做市场的同僚应该留意的16个字
  • 威联通_[Zheli体验] 威联通入门折腾小记
  • python随机生成50个整数奇数位降序_15个经典函数公式,易学易用,收藏备用
  • Web2.0为互联网行业带来的又一次变革
  • 和画意思相近的字_王雪涛:画花鸟,这几条要切记!(附高清绝美花鸟60幅)...
  • Windows live服务终于来了!
  • 双显示器 启动黑屏 黑苹果_黑苹果装机黑屏问题解决方案5500xt
  • oracle查询bolb字段_如何在Oracle SQL Developer中查看Blob数据
  • 打造全新的Windows Live™ Spaces
  • cdh sqoop 配置_Navigator 配置定期删除管理元数据
  • perl 新得
  • token干什么用_什么是TOKEN?Token小号的理解运用,拼多多,知乎,快手,抖音的Token是什么意思...
  • GCC for Win32 开发环境介绍(3)
  • m3u8手机批量转码_阿里云视频转码批量提交(mp4 to m3u8)
  • 什么项目可以发布?一切竭可外包——CSDN外包频道(57)
  • 深入了解以太坊
  • 2017 前端面试准备 - 收藏集 - 掘金
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • CODING 缺陷管理功能正式开始公测
  • js操作时间(持续更新)
  • Logstash 参考指南(目录)
  • 百度地图API标注+时间轴组件
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 多线程事务回滚
  • 今年的LC3大会没了?
  • 如何编写一个可升级的智能合约
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 无服务器化是企业 IT 架构的未来吗?
  • 线上 python http server profile 实践
  • 想写好前端,先练好内功
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 自制字幕遮挡器
  • Spring Batch JSON 支持
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (23)Linux的软硬连接
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (二)WCF的Binding模型
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (全注解开发)学习Spring-MVC的第三天
  • (一)SpringBoot3---尚硅谷总结
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转)项目管理杂谈-我所期望的新人
  • *p++,*(p++),*++p,(*p)++区别?
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET 分布式技术比较
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • .sh 的运行
  • /bin/bash^M: bad interpreter: No such file ordirectory
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @font-face 用字体画图标