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

python 中BeautifulSoup入门

在前面的例子用,我用了BeautifulSoup来从58同城抓取了手机维修的店铺信息,这个库使用起来的确是很方便的。本文是BeautifulSoup 的一个详细的介绍,算是入门把。文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/

什么是BeautifulSoup?

Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间。

直接看例子:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>

"""

soup = BeautifulSoup(html_doc)

print soup.title

print soup.title.name

print soup.title.string

print soup.p

print soup.a

print soup.find_all('a')

print soup.find(id='link3')

print soup.get_text()

结果为:

<title>The Dormouse's story</title>
title
The Dormouse's story
<p class="title"><b>The Dormouse's story</b></p>
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

可以看出:soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p  得到的是文档中的第一个p标签,要想得到所有标签,得用find_all

函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.

get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的。你可以试试 print soup.p.get_text()

其实是可以获得标签的其他属性的,比如我要获得a标签的href属性的值,可以使用 print soup.a['href'],类似的其他属性,比如class也是可以这么得到的(soup.a['class'])。

特别的,一些特殊的标签,比如head标签,是可以通过soup.head 得到,其实前面也已经说了。

如何获得标签的内容数组?使用contents 属性就可以 比如使用 print soup.head.contents,就获得了head下的所有子孩子,以列表的形式返回结果,

可以使用 [num]  的形式获得 ,获得标签,使用.name 就可以。

获取标签的孩子,也可以使用children,但是不能print soup.head.children 没有返回列表,返回的是 <listiterator object at 0x108e6d150>,

不过使用list可以将其转化为列表。当然可以使用for 语句遍历里面的孩子。

关于string属性,如果超过一个标签的话,那么就会返回None,否则就返回具体的字符串print soup.title.string 就返回了 The Dormouse's story

超过一个标签的话,可以试用strings

向上查找可以用parent函数,如果查找所有的,那么可以使用parents函数

查找下一个兄弟使用next_sibling,查找上一个兄弟节点使用previous_sibling,如果是查找所有的,那么在对应的函数后面加s就可以

如何遍历树?

 使用find_all 函数

find_all(nameattrsrecursivetextlimit**kwargs)

举例说明:

print soup.find_all('title')
print soup.find_all('p','title')
print soup.find_all('a')
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值为:

[<title>The Dormouse's story</title>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

通过css查找,直接上例子把:

print soup.find_all("a", class_="sister")
print soup.select("p.title")

通过属性进行查找
print soup.find_all("a", attrs={"class": "sister"})

通过文本进行查找
print soup.find_all(text="Elsie")
print soup.find_all(text=["Tillie", "Elsie", "Lacie"])

限制结果个数
print soup.find_all("a", limit=2)

结果为:

[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[u'Elsie']
[u'Elsie', u'Lacie', u'Tillie']
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

总之,通过这些函数可以查找到想要的东西。

---end---

相关文章:

  • Python 标准库 urllib2 的使用细节
  • Python BeautifulSoup 简单笔记
  • 痘痘,多少事你不知道自己不知道?
  • nginx配置免费的ssl证书,支持https安全访问
  • HID攻击之TEENSY实战 -- 天融信阿尔法实验室-冷风
  • Metasploit Framework 和 PostgreSQL
  • CVE-2015-0235:Linux Glibc幽灵漏洞允许黑客远程获取系统权限
  • 一行代码检测杀毒软件是否正常工作
  • 支持 Mac OS X Yosemite 10.10 的JAVA 6
  • Apple - Open Source
  • snmp渗透辅助脚本两则
  • Mac 安装 Crypto.Cipher 模块
  • 一枚PHP最新版本过狗Shell
  • Web安全漏洞
  • Error: Failure while executing: git checkout -q master (错误解决)
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【剑指offer】让抽象问题具体化
  • 2017届校招提前批面试回顾
  • python学习笔记 - ThreadLocal
  • Python语法速览与机器学习开发环境搭建
  • Spring核心 Bean的高级装配
  • webpack+react项目初体验——记录我的webpack环境配置
  • 阿里云应用高可用服务公测发布
  • 后端_ThinkPHP5
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 区块链共识机制优缺点对比都是什么
  • 如何使用 JavaScript 解析 URL
  • 少走弯路,给Java 1~5 年程序员的建议
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 译米田引理
  • 2017年360最后一道编程题
  • 函数计算新功能-----支持C#函数
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (2022 CVPR) Unbiased Teacher v2
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (9)STL算法之逆转旋转
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (四)鸿鹄云架构一服务注册中心
  • (转)mysql使用Navicat 导出和导入数据库
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .net core 6 redis操作类
  • .NET大文件上传知识整理
  • .NET基础篇——反射的奥妙
  • @Repository 注解
  • [100天算法】-目标和(day 79)
  • [Android Studio] 开发Java 程序
  • [android] 天气app布局练习
  • [BZOJ3211]:花神游历各国(小清新线段树)
  • [C#]C# winform部署yolov8目标检测的openvino模型
  • [C#]winform部署yolov5-onnx模型
  • [CSDN首发]鱿鱼游戏的具体玩法详细介绍