python爬虫beautifulsoup解析html方法


Posted in Python onDecember 07, 2020

用BeautifulSoup 解析html和xml字符串

python爬虫beautifulsoup解析html方法

实例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import re

#待分析字符串
html_doc = """
<html>
<head>
  <title>The Dormouse's story</title>
</head>
<body>
<p class="title aq">
  <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" rel="external nofollow" class="sister" id="link1">Elsie</a>,
  <a href="http://example.com/lacie" rel="external nofollow" class="sister" id="link2">Lacie</a> 
  and
  <a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
  and they lived at the bottom of a well.
</p>

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


# html字符串创建BeautifulSoup对象
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

#输出第一个 title 标签
print soup.title

#输出第一个 title 标签的标签名称
print soup.title.name

#输出第一个 title 标签的包含内容
print soup.title.string

#输出第一个 title 标签的父标签的标签名称
print soup.title.parent.name

#输出第一个 p 标签
print soup.p

#输出第一个 p 标签的 class 属性内容
print soup.p['class']

#输出第一个 a 标签的 href 属性内容
print soup.a['href']
'''
soup的属性可以被添加,删除或修改. 再说一次, soup的属性操作方法与字典一样
'''
#修改第一个 a 标签的href属性为 http://www.baidu.com/
soup.a['href'] = 'http://www.baidu.com/'

#给第一个 a 标签添加 name 属性
soup.a['name'] = u'百度'

#删除第一个 a 标签的 class 属性为
del soup.a['class']

##输出第一个 p 标签的所有子节点
print soup.p.contents

#输出第一个 a 标签
print soup.a

#输出所有的 a 标签,以列表形式显示
print soup.find_all('a')

#输出第一个 id 属性等于 link3 的 a 标签
print soup.find(id="link3")

#获取所有文字内容
print(soup.get_text())

#输出第一个 a 标签的所有属性信息
print soup.a.attrs


for link in soup.find_all('a'):
  #获取 link 的 href 属性内容
  print(link.get('href'))

#对soup.p的子节点进行循环输出  
for child in soup.p.children:
  print(child)

#正则匹配,名字中带有b的标签
for tag in soup.find_all(re.compile("b")):
  print(tag.name)

爬虫设计思路:

python爬虫beautifulsoup解析html方法

详细手册:

https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

到此这篇关于python爬虫beautifulsoup解析html方法 的文章就介绍到这了,更多相关beautifulsoup解析html内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
使用Python的Tornado框架实现一个一对一聊天的程序
Apr 25 Python
python爬虫实现教程转换成 PDF 电子书
Feb 19 Python
Python的IDEL增加清屏功能实例
Jun 19 Python
用Python分析3天破10亿的《我不是药神》到底神在哪?
Jul 12 Python
pycharm使用matplotlib.pyplot不显示图形的解决方法
Oct 28 Python
Python编写合并字典并实现敏感目录的小脚本
Feb 26 Python
PySide和PyQt加载ui文件的两种方法
Feb 27 Python
python飞机大战pygame碰撞检测实现方法分析
Dec 17 Python
Python递归求出列表(包括列表中的子列表)的最大值实例
Feb 27 Python
python能否java成为主流语言吗
Jun 22 Python
利用PyTorch实现VGG16教程
Jun 24 Python
python实现录制全屏和选择区域录屏功能
Feb 05 Python
python可视化 matplotlib画图使用colorbar工具自定义颜色
Dec 07 #Python
用ldap作为django后端用户登录验证的实现
Dec 07 #Python
Django中使用Celery的方法步骤
Dec 07 #Python
python集合的新增元素方法整理
Dec 07 #Python
python进行二次方程式计算的实例讲解
Dec 06 #Python
paramiko使用tail实时获取服务器的日志输出详解
Dec 06 #Python
python中二分查找法的实现方法
Dec 06 #Python
You might like
FirePHP 推荐一款PHP调试工具
2011/04/23 PHP
php数组索引的Key加引号和不加引号的区别
2014/08/19 PHP
DWZ+ThinkPHP开发时遇到的问题分析
2016/12/12 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
2017/10/23 PHP
在浏览器窗口上添加遮罩层的方法
2012/11/12 Javascript
jQuery实现表单提交时判断的方法
2014/12/13 Javascript
javascript html5移动端轻松实现文件上传
2020/03/27 Javascript
canvas滤镜效果实现代码
2017/02/06 Javascript
react native带索引的城市列表组件的实例代码
2017/08/08 Javascript
利用SpringMVC过滤器解决vue跨域请求的问题
2018/02/10 Javascript
浅谈webpack打包过程中因为图片的路径导致的问题
2018/02/21 Javascript
Vue自定义弹窗指令的实现代码
2018/08/13 Javascript
nodejs基础之buffer缓冲区用法分析
2018/12/26 NodeJs
Node.js JSON模块用法实例分析
2019/01/04 Javascript
react 组件传值的三种方法
2019/06/03 Javascript
在vue中实现禁止回退上一步,路由不存历史记录
2020/07/22 Javascript
浅谈Vue开发人员的7个最好的VSCode扩展
2021/01/20 Vue.js
[00:34]DOTA2上海特级锦标赛 VG战队宣传片
2016/03/04 DOTA
[09:31]2016国际邀请赛中国区预选赛Yao赛后采访 答题送礼
2016/06/27 DOTA
Python闭包之返回函数的函数用法示例
2018/01/27 Python
python tkinter界面居中显示的方法
2018/10/11 Python
对python 读取线的shp文件实例详解
2018/12/22 Python
Python使用tkinter模块实现推箱子游戏
2019/10/08 Python
Python3 读取Word文件方式
2020/02/13 Python
Python使用requests xpath 并开启多线程爬取西刺代理ip实例
2020/03/06 Python
python编写扎金花小程序的实例代码
2021/02/23 Python
HTML5 解析规则分析
2009/08/14 HTML / CSS
通信工程毕业生自荐信
2013/11/01 职场文书
毕业自我评价范文
2013/11/17 职场文书
十佳党员事迹材料
2014/08/28 职场文书
九一八事变演讲稿
2014/09/05 职场文书
2014大学生中国梦主题教育学习思想汇报
2014/09/10 职场文书
先进集体事迹材料范文
2014/12/25 职场文书
2015年领班工作总结
2015/04/29 职场文书
MySQL如何快速创建800w条测试数据表
2022/03/17 MySQL
基于Redis6.2.6版本部署Redis Cluster集群的问题
2022/04/01 Redis