阅读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 ...
随机推荐
- 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)
Let SSS be a sequence of integers s1s_{1}s1, s2s_{2}s2, ........., sns_{n}sn Each integer i ...
- 【转载】 如何看待 2019 年 CS PhD 现扎堆申请且大部分为 AI 方向?未来几年 AI 泡沫会破裂吗?
原贴地址: https://www.zhihu.com/question/316135639 作为一个 AI 方向的在读博士生,实在是过的蛮闹心,无意中逛知乎发现了这个帖子,发现很适合现在的自己,于是 ...
- 使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"
PHPMailer项目地址:https://github.com/PHPMailer/PHPMailer 项目中用到PHPMailer,使用过程中报错:"Connection failed. ...
- python基于并发与socket实现远程文件传输程序
FTP程序 Client: * bin/start.py 程序入口 * conf/配置文件存放 * core/ * auth.py 登陆,注册以及上传下载查看当前文件夹下文件以及删除功能存放 * cl ...
- Java调用Lua脚本(热载实现)
前言: Lua作为解析执行的脚本语言, 往往是易变逻辑编写的首选语言, 尤其是在游戏领域. C/C++和Lua的结合, 往往了标配. 比如Redis, Nginx其对Lua的支持, 也是杠杠的. 当然 ...
- [LeetCode&Python] Problem 237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode&Python] Problem 1: Two Sum
Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...
- Unity等待写入数据
EasyTouch中QuickGesture的用法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Qu ...
- Gym.101908 Brazil Subregional Programming Contest(寒假自训第六场)
这几天睡眠时间都不太够,室友晚上太会折腾了,感觉有点累,所以昨天的题解也没写,看晚上能不能补起来. B . Marbles 题意:给定N组数(xi,yi),玩家轮流操作,每次玩家可以选择其中一组对其操 ...
- java关键字详解----static
Java Static关键字详解 提起static关键字,相信大家绝对不会陌生,但是,想要完全说明白,猛的一想,发现自己好像又说不太明白... ...比方说,昨天被一个同学问起的时候... ... ...