阅读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的更多相关文章

  1. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...

  2. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...

  3. 首部讲Python爬虫电子书 Web Scraping with Python

    首部python爬虫的电子书2015.6pdf<web scraping with python> http://pan.baidu.com/s/1jGL625g 可直接下载 waterm ...

  4. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  5. <Web Scraping with Python>:Chapter 1 & 2

    <Web Scraping with Python> Chapter 1 & 2: Your First Web Scraper & Advanced HTML Parsi ...

  6. 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)

  7. Web Scraping with Python

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  8. 《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 ...

  9. 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 ...

随机推荐

  1. 莫烦tensorflow(3)-Variable

    import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.ad ...

  2. SQL注入之Sqli-labs系列第二十六关(过滤空格、注释符、逻辑运算符注入)和第二十六A

    开始挑战第二十六关(Trick with comments and space) 0x1看看源代码 (1)过滤了#  or and  /**/  /  \ ,通过判断也过滤了空格 (2)这样一来只能看 ...

  3. 大数据-06-Spark之读写Hive数据

    简介 Hive中的表是纯逻辑表,就只是表的定义等,即表的元数据.Hive本身不存储数据,它完全依赖HDFS和MapReduce.这样就可以将结构化的数据文件映射为为一张数据库表,并提供完整的SQL查询 ...

  4. 【转载】 AI会议的总结(by南大周志华)

    原文地址: https://blog.csdn.net/LiFeitengup/article/details/8441054 最近在查找期刊会议级别的时候发现这篇博客,应该是2012年之前的内容,现 ...

  5. word2vec原理推导与代码分析

    https://github.com/hankcs/HanLP/wiki/word2vec http://www.hankcs.com/nlp/word2vec.html Hanlp word2vec ...

  6. 利用scrapy下载图片保存到本地

    1.先声明一下,起始位置已经是将所有的图片链接都能到pipelines.py中 2.创建一个类,继承于ImagesPipeline,因此也就需要导入ImagesPipeline from scrapy ...

  7. Django中的应用

    1.什么是应用 应用就是网站中的一个独立的程序模块,在Django中,主目录一般不处理具体的请求,主目录一般要处理的是项目的初始化操作以及请求的分发(分布式请求处理).而具体的请求是由各个应用去处理. ...

  8. [LeetCode&Python] Problem 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  9. Unity 3D动态修改Shader状态,使物体透明等等

    Unity动态改Shader状态透明 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  10. 超链接(空链接-target-title属性)

    空链接:<a href="#">内容</a> 超链接标签: <a href=" " >内容</a> 属性: hr ...