python BeautifulSoup4解析网页
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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></body></html>
""" soup=BS(html,'html.parser') for i in soup.find_all('a'):
print('i.text:',i.text)#注释掉的内容就不打印了 str类型
print('i.string:',i.string) #注释掉的内容 都会打印出来,NavigableString对象 print('soup.head.contents:',soup.head.contents,type(soup.head.contents))
print('soup.head.children:',soup.head.children,type(soup.head.children)) print('soup.body.contents:',soup.body.contents)#返回一个子元素的列表
print('soup.body.children:',soup.body.children)#返回一个子元素的迭代器 for i in soup.body.children:
print(i) print('子孙节点 都显示出来')
for i in soup.body.descendants:
print(i) print('soup.body.string:',soup.body.string)
print('soup.body.strings:',soup.body.strings)
print('soup.body.stripped_strings:',soup.body.stripped_strings) #过滤掉所有空格显示 print('去掉空格的body子元素:')
for i in soup.body.stripped_strings:
print(i) print('soup.a.parent:',soup.a.parent)
print('soup.a.next_sibling:',soup.a.next_sibling) #注意文本节点、换行\n都可能成为当前节点的上一个或者下一个同级节点
print('soup.a.previous_sibling:',soup.a.previous_sibling)
print('soup.a.next_element:',soup.a.next_element) #下一个元素 不一定同级
print('soup.a.previous_element:',soup.a.previous_element) print('打印所有后面的同级节点:\n')
for i in soup.a.next_siblings:
print(i) print('soup.a.next_element:',list(soup.a.next_elements)[1]) print('***********find_all*****') print(soup.find_all('a')) print('引入正则表达式:') import re
print(soup.find_all(re.compile(r'^title'))) #正则匹配的是 标签的名字 print('列表的方式匹配:')
print(soup.find_all(['a','b'])) print('函数的方式匹配,类似filter')
def func(tag):
if tag.has_attr('class') and re.search(r'^a',tag.name):
return tag print(soup.find_all(func)) html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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></body></html>
""" soup=BS(html,'html.parser') print('按属性值查找:')
print(soup.find_all(id='link1'))
print(soup.find_all('a',id='link1')) print(soup.find_all(id='link2',href=re.compile(r'laci'))) #返回的都是列表
print(soup.find_all(class_='story')) #注意后面加的下划线
print(soup.find_all(attrs={'class':'sister'})) print('按元素内容查找text参数:')
print(soup.find_all(text='Tillie'))
print(soup.find_all(text=['Tillie','Lacie'])) #返回的都是元素内容
print(soup.find_all(text=re.compile(r'ormous'))) print('通过内容元素 找到上级元素')
print(soup.find_all(text=re.compile(r'ormous'))[1].parent.parent) #限制查找数量
print('limit:')
print(soup.find_all('a',limit=2)) print('只在子节点查找:')
print(soup.body.find_all('a',limit=2,recursive=False)) #只查找子节点 recursive循环的、递归的
print(soup.body.find_all(class_='story',recursive=False))
python BeautifulSoup4解析网页的更多相关文章
- Python爬虫解析网页的4种方式 值得收藏
用Python写爬虫工具在现在是一种司空见惯的事情,每个人都希望能够写一段程序去互联网上扒一点资料下来,用于数据分析或者干点别的事情. 我们知道,爬虫的原理无非是把目标网址的内容下载下来存储到内存 ...
- python bs4解析网页时 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to inst(转)
Python小白,学习时候用到bs4解析网站,报错 bs4.FeatureNotFound: Couldn't find a tree builder with the features you re ...
- 使用Python中的urlparse、urllib抓取和解析网页(一)(转)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过Python 语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- python网络爬虫之解析网页的BeautifulSoup(爬取电影图片)[三]
目录 前言 一.BeautifulSoup的基本语法 二.爬取网页图片 扩展学习 后记 前言 本章同样是解析一个网页的结构信息 在上章内容中(python网络爬虫之解析网页的正则表达式(爬取4k动漫图 ...
- Python中的urlparse、urllib抓取和解析网页(一)
对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过Python 语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...
- python网络爬虫之解析网页的XPath(爬取Path职位信息)[三]
目录 前言 XPath的使用方法 XPath爬取数据 后言 @(目录) 前言 本章同样是解析网页,不过使用的解析技术为XPath. 相对于之前的BeautifulSoup,我感觉还行,也是一个比较常用 ...
- python网络爬虫-解析网页(六)
解析网页 主要使用到3种方法提取网页中的数据,分别是正则表达式.beautifulsoup和lxml. 使用正则表达式解析网页 正则表达式是对字符串操作的逻辑公式 .代替任意字符 . *匹配前0个或多 ...
- Python爬虫之解析网页
常用的类库为lxml, BeautifulSoup, re(正则) 以获取豆瓣电影正在热映的电影名为例,url='https://movie.douban.com/cinema/nowplaying/ ...
- [技术博客] BeautifulSoup4分析网页
[技术博客] BeautifulSoup4分析网页 使用BeautifulSoup4进行网页文本分析 前言 进行网络爬虫时我们需要从网页源代码中提取自己所需要的信息,分析整理后存入数据库中. 在pyt ...
随机推荐
- selenium3+Python3+sublime text3自动化登录
前言: 对于初学者来说,python自带的IDLE,精简又方便,不过一个好的编辑器能让python编码变得更方便,更加优美些. 不过呢,也可以自己去下载其他更好用的代码编辑器,在这推荐: PyChar ...
- 【GStreamer开发】GStreamer基础教程04——时间管理
目标 本教程主要讲述一些和时间相关的内容.主要包括: 1. 如何问pipeline查询到流的总时间和当前播放的时间 2. 如何在流内部实现跳转功能 介绍 GstQuery是向一个element或者pa ...
- Hive 企业调优
9.企业级调优 9.1 Fetch 抓取 Fetch 抓取:Hive 中对某些情况的查询可以不必使用 MapReduce 计算: hive.fetch.task.conversion:more 9.2 ...
- webpack to package typescript & scss
Demo2操作手册 本Demo演示如何配合各种loader进行稍复杂的使用 准备环境 初始化环境, cd到demo目录之后, 执行如下命令: npm init -y npm install webpa ...
- [转帖]MySQL语句大全
MySQL语句大全 https://www.cnblogs.com/jicki/p/5548676.html 一.连接mysql. 格式: mysql -h主机地址 -u用户名 -p用户密码 二.修改 ...
- 洛谷P4145——上帝造题的七分钟2 / 花神游历各国
题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...
- Springmvc在项目启动时查询数据库并初始化静态变量
private static List<ResourceEntity> resourceList = null; //初始化的全局静态变量 @Autowired private Resou ...
- FZU2018级算法第五次作业 missile(排序+枚举)
在解题报告之前,首先对同一次作业中另外一题(求逆序对)某人在未经冰少允许情况下,擅自登录冰少账号,原模原样剽窃冰少代码,并且最终还被评为优秀作业的行为表示严正抗议! 题目大意: 二维平面上给出 n 个 ...
- Spring Boot使用@Value注解获取配置文件中的属性
获取配置文件的内容——
- 立体像对空间前方交会-共线方程求解法(python实现)
一.原理 二.步骤 a.用各自像片的角元素计算出左右像片的旋转矩阵R1和R2. b.有同名像点列出共线方程. c.将方程写为未知数的线性方程形式,计算线性系数. d.写出误差方程,系数矩阵与常数项. ...