BeautifulSoup-find,findAll
BeautifulSoup的主要函数使用
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id="hehe"><b>The Dormouse's story</b></p>
<p class="story" id="firstpara">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,'html.parser')
print soup.prettify()
这里是一个读取html标签 然后通过prettify()函数输出标签的过程。这里输出soup对象的html标签有多种方法:
1 soup.prettify()
2 soup.html
3 soup.contents
4 soup
另外使用soup+标签名称可以获取html标签中第一个匹配的标签内容,举例:
print soup.p输出结果为:<p class="title"><b>The Dormouse's story</b></p>
print soup.p.string 输出标签的内容 结果为:The Dormouse's story
另外输出标签内容还可以使用get_text()函数:
pid = soup.find(href=re.compile("^http:")) #使用re正则匹配 后面有讲
p1=soup.p.get_text()
The Dormouse's story
通过get函数获得标签的属性:
soup=BeautifulSoup(html,'html.parser')
pid = soup.findAll('a',{'class':'sister'})
for i in pid:
print i.get('href') #对每项使用get函数取得tag属性值
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie
对其他的标签也是同样可用的,并且输出的结果为文档中第一个匹配的对象,如果要搜索其他的标签需要使用find findAll函数。
BeautifulSoup提供了强大的搜索函数find 和findall,这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效。
findAll(name, attrs, recursive, text, limit, **kwargs)
for link in soup.find_all('a'): #soup.find_all返回的为列表
print(link.get('href'))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie
findAll也可以使用标签的属性搜索标签,寻找 id=”secondpara”的 p 标记,返回一个结果集:
> pid=soup.findAll('p',id='hehe') #通过tag的id属性搜索标签
> print pid
[<p class="title" id="hehe"><b>The Dormouse's story</b></p>]
>pid = soup.findAll('p',{'id':'hehe'}) #通过字典的形式搜索标签内容,返回的为一个列表[]
>print pid
[<p class="title" id="hehe"><b>The Dormouse's story</b></p>]
利用正则表达式搜索tag标签内容:
>pid=soup.findAll(id=re.compile("he$")) #正则表达式的使用
>print pid
[<p class="title" id="hehe"><b>The Dormouse's story</b></p>]
利用标签的多个属性值进行搜索:
pp=soup.findAll('a',attrs={'href':re.compile('^http'),'id':'link1'}) #标签多个属性值进行搜索 这里的attrs不可省略,便签'a'是可以省略的 相当于一个限定标签符
print pp
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>] #输出结果为list
对搜索结果的个数进行限制: limit=n
pid = soup.findAll('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>]
利用find_all搜索返回一个列表:
soup.find_all(["a", "b"])
# [<b>The Dormouse's story</b>,
# <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>]
这里的find_all函数参数中设置了一个列表的形式,包含了a和b两个标签,使结果以列表的形式返回。
读取和修改属性:
> p1 = soup.p
> p1 #输出p1内容
<p id="firstpara" align="center">This is paragraph<b>one</b>.</p>
> p1['id'] #输出p1的id属性
hehe
>p1['id']='haha' #修改p1的id属性值
>print p1['id']
haha
BeautifulSoup中的find和findAll用法相同,不同之处为find返回的是findAll搜索值的第一个值。举例:
>soup=BeautifulSoup(html,'html.parser')
>pid = soup.find(href=re.compile("^http:")) #这里也是使用re正则匹配
>print pid
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
BeautifulSoup-find,findAll的更多相关文章
- BeautifulSoup随笔
BeautifulSoup是一个类 b = BeautifulSoup(html) b对象有与html结构相关的各种方法和和属性. a = b.findAll('a')获得标签的对象 a对象又有关于属 ...
- python+selenium+webdriver+BeautifulSoup实现自动登录
from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...
- python笔记之提取网页中的超链接
python笔记之提取网页中的超链接 对于提取网页中的超链接,先把网页内容读取出来,然后用beautifulsoup来解析是比较方便的.但是我发现一个问题,如果直接提取a标签的href,就会包含jav ...
- python去掉html标签
s = '<SPAN style="FONT- SIZE: 9pt">开始1~3<SPAN lang=EN-US>& lt;?xml:namespa ...
- python 站点爬虫 下载在线盗墓笔记小说到本地的脚本
近期闲着没事想看小说,找到一个全是南派三叔的小说的站点,决定都下载下来看看,于是动手,在非常多QQ群里高手的帮助下(本人正則表達式非常烂.程序复杂的正则都是一些高手指导的),花了三四天写了一个脚本 须 ...
- 读取指定页面中的超链接-Python 3.7
#!/usr/bin/env python#coding: utf-8from bs4 import BeautifulSoupimport urllibimport urllib.requestim ...
- 《恶魔人crybaby》豆瓣短评爬取
作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3159 爬虫综合大作业 选择一个热点或者你感兴趣的主题. 选择爬取的对象 ...
- 小白如何入门 Python 爬虫?
本文针对初学者,我会用最简单的案例告诉你如何入门python爬虫! 想要入门Python 爬虫首先需要解决四个问题 熟悉python编程 了解HTML 了解网络爬虫的基本原理 学习使用python爬虫 ...
- python学习之----BeautifulSoup的find()和findAll()及四大对象
BeautifulSoup 里的find() 和findAll() 可能是你最常用的两个函数.借助它们,你可以通 过标签的不同属性轻松地过滤HTML 页面,查找需要的标签组或单个标签. 这两个函数非常 ...
- BeautifulSoup的find()和findAll()
BeautifulSoup的提供了两个超级好用的方法(可能是你用bs方法中最常用的).借助这两个函数,你可以通过表现的不同属性轻松过滤HTML(XML)文件,查找需要的标签组或单个标签. 首先find ...
随机推荐
- (转)JavaScript中的运算符优先级
JavaScript中的运算符优先级是一套规则.该规则在计算表达式时控制运算符执行的顺序.具有较高优先级的运算符先于较低优先级的运算符执行.例如,乘法的执行先于加法. 下表按从最高到最低的优先级列出J ...
- AFNetworking到底做了什么?(二)
接着上一篇的内容往下讲,如果没看过上一篇内容可以点这: AFNetworking到底做了什么? 之前我们讲到NSUrlSession代理这一块: 代理8: /* task完成之后的回调,成功和失败 ...
- 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)
上篇文章<深入浅出Mybatis系列(一)---Mybatis入门>, 写了一个Demo简单体现了一下Mybatis的流程.本次,将简单介绍一下Mybatis的配置文件: 上次例子中,我们 ...
- python之萌新入门的第一天
新人的挑战一:配置环境变量及正确地使用工具. 配置环境好难,照着教程做很简单,但是确定环境配置成功好难. 一开始运行helloworld程序的文件时一直出现SyntaxError:invaild sy ...
- ORBSLAM2与OPENCV3.1.0出错解决办法
用opencv3.1.0做ORBSLAM2运行一下命令时cd ORB_SLAM2 chmod +x build.sh ./build.sh出错:/usr/bin/ld: CMakeFiles/mono ...
- POJ 3126 Prime Path
给定两个四位素数a b,要求把a变换到b 变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能 ...
- MongoDB aggregate 运用篇 个人总结
最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得.. 别人写过的我就不过多描述了,大家一搜能搜索到N多一样的,我写一 ...
- [css]零散的重构知识
1.不建议这样写 font-family:"微软雅黑"; 建议 font-family:Microsoft YaHei; 因为有些网站不兼容中文(GB2312)的字符
- reversing-Easy Crack
Easy Crack 程序启动后输入任意字符会显示一个MessageBox的Incorrect Password. 打开OllyDbg,载入程序后查找到目标字符串Incorrect Password, ...
- Codeforces Round #383 (Div. 1)
A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...