阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

1..BeautifulSoup库的使用
BeautifulSoup通常用来分析爬虫抓取的Web文档。
其中findAll函数的使用情景:
链接:http://www.pythonscraping.com/pages/warandpeace.html 中内容如下:

文字部分有黑色,红色,和绿色的,其决定因素主要在于其中的:

“<span class=”red”>
“<span class=”green”>
实现功能:提取出这篇文章中的所有绿色文字。
代码如下:
# -*- coding: utf-8 -*-
"""
Spyder Editor This is a temporary script file.
""" from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
bsObj = BeautifulSoup(html,"lxml")
nameList = bsObj.findAll("span",{"class":"green"})
for name in nameList:
print(name.get_text())
代码运行结果:
Anna
Pavlovna Scherer
Empress Marya
Fedorovna
Prince Vasili Kuragin
Anna Pavlovna
St. Petersburg
the prince
Anna Pavlovna
Anna Pavlovna
the prince
the prince
the prince
Prince Vasili
Anna Pavlovna
Anna Pavlovna
the prince
Wintzingerode
King of Prussia
le Vicomte de Mortemart
Montmorencys
Rohans
Abbe Morio
the Emperor
the prince
Prince Vasili
Dowager Empress Marya Fedorovna
the baron
Anna Pavlovna
the Empress
the Empress
Anna Pavlovna's
Her Majesty
Baron
Funke
The prince
Anna
Pavlovna
the Empress
The prince
Anatole
the prince
The prince
Anna
Pavlovna
Anna Pavlovna
结果分析:提取出了文中所有绿色文字的内容。
关于bsObj.findAll(tagName,tagAttributes)的调用
.findAll()最常用的参数为:tagName,tagAttributes
tagName指的是"h1","h2","h3"之类的标签
tagAttributes是一个字典类型的数据,指的是{"class":"green","class":"red"}之类的数据。
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll的更多相关文章
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...
- 首部讲Python爬虫电子书 Web Scraping with Python
首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...
- Web Scraping with Python读书笔记及思考
Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...
- <Web Scraping with Python>:Chapter 1 & 2
<Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...
- Web scraping with Python (part II) « Jean, aka Sig(gg)
Web scraping with Python (part II) « Jean, aka Sig(gg) Web scraping with Python (part II)
- Web Scraping with Python
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
- 《Web Scraping With Python》Chapter 2的学习笔记
You Don't Always Need a Hammer When Michelangelo was asked how he could sculpt a work of art as mast ...
- Web Scraping using Python Scrapy_BS4 - using BeautifulSoup and Python
Use BeautifulSoup and Python to scrap a website Lib: urllib Parsing HTML Data Web scraping script fr ...
随机推荐
- thrift使用案例
参考资料:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ 首先是定义thrift IDL接口,如下(SunTelTc.thri ...
- XXS level4
(1)查看PHP源代码 <?php ini_set("display_errors", 0); $str = $_GET["keyword"]; $str ...
- windows apache "The requested operation has failed" 启动失败
找到失败原因,进入cmd(win+r快捷键,输入cmd)命令行下 进入到你的apache bin目录下: 每个人错误可能不同,根据自己问题去相应改
- Kafka设计解析:Kafka High Availability
Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永远不能再恢复,亦 ...
- centos6安装opencv2
环境 centos6.5 opencv2.4 python2.6(centos自带的) 下载 opencv可从GitHub下载https://github.com/opencv/opencv/tree ...
- hdu4847 Wow! Such Doge! KMP
Chen, Adrian (November 7, 2013). “Doge Is An Ac- tually Good Internet Meme. Wow.”. Gawker. Retrieved ...
- Go Example--变量
package main import "fmt" //通过import导入fmt标准包 func main() { //定义变量,并初始化 var a string = &quo ...
- 【JVM】参数配置
[一]JVM参数配置释意 编号 配置项 例子 含义 备注 1 -Xmx -Xmx20m java应用最大可用内存为20M 整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小.持久代一般固 ...
- mysql自增主键
MariaDB [test]> create table test1(id int primary key auto_increment,name varchar(20))auto_increm ...
- MySQL--派生表Condition Pushdown优化
如果派生表外部过滤条件可以下推到派生表内部,可以有效减少派生表内部扫描数据量和派生表使用内存甚至避免使用派生表. 如对于下面查询: SELECT * FROM ( SELECT cluster_id, ...