Beautiful Soup第三方爬虫插件

什么是BeautifulSoup?

Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。它可以大大节省你的编程时间。

安装Beautiful Soup

Beautiful Soup的下载地址:https://www.crummy.com/software/BeautifulSoup/bs4/download/4.4/

将下载的beautifulsoup4-4.4.1.tar.gz解压,进入beautifulsoup4-4.4.1目录,执行命令:python setup.py install

[email protected]:~/soft/python-source/beautifulsoup4-4.4.1$ python setup.py installTraceback (most recent call last):
  File "setup.py", line 1, in <module>
    from setuptools import (
ImportError: No module named setuptools
[email protected]:~/soft/python-source/beautifulsoup4-4.4.1$ 

出错了,需要安装setuptools

下载地址,https://pypi.python.org/pypi/setuptools#downloads,页面的下方有安装包下载

$ tar -zxvf beautifulsoup4-4.4.1.tar.gz
$ cd beautifulsoup4-4.4.1
$ python setup.py install

在执行一次beautifulsoup的安装

[email protected]:~/soft/python-source/beautifulsoup4-4.4.1$ sudo python setup.py install
running install
running bdist_egg
running egg_info
writing requirements to beautifulsoup4.egg-info/requires.txt
...

测试下

[email protected]:~/soft/python-source/beautifulsoup4-4.4.1$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> 

在python环境中引入BeautifulSoup包,如上所示,说明成功了。

直接看例子:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse‘s story</title></head>
<body>
<p class="title"><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>

"""

soup = BeautifulSoup(html_doc)

print soup.title

print soup.title.name

print soup.title.string

print soup.p

print soup.a

print soup.find_all(‘a‘)

print soup.find(id=‘link3‘)

print soup.get_text()

结果为:

<title>The Dormouse‘s story</title>
title
The Dormouse‘s story
<p class="title"><b>The Dormouse‘s story</b></p>
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<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>]
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

The Dormouse‘s story
The Dormouse‘s story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

可以看出:soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p  得到的是文档中的第一个p标签,要想得到所有标签,得用find_all

函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.

get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的。你可以试试 print soup.p.get_text()

其实是可以获得标签的其他属性的,比如我要获得a标签的href属性的值,可以使用 print soup.a[‘href‘],类似的其他属性,比如class也是可以这么得到的(soup.a[‘class‘])。

特别的,一些特殊的标签,比如head标签,是可以通过soup.head 得到,其实前面也已经说了。

如何获得标签的内容数组?使用contents 属性就可以 比如使用 print soup.head.contents,就获得了head下的所有子孩子,以列表的形式返回结果,

可以使用 [num]  的形式获得 ,获得标签,使用.name 就可以。

获取标签的孩子,也可以使用children,但是不能print soup.head.children 没有返回列表,返回的是 <listiterator object at 0x108e6d150>,

不过使用list可以将其转化为列表。当然可以使用for 语句遍历里面的孩子。

关于string属性,如果超过一个标签的话,那么就会返回None,否则就返回具体的字符串print soup.title.string 就返回了 The Dormouse‘s story

超过一个标签的话,可以试用strings

向上查找可以用parent函数,如果查找所有的,那么可以使用parents函数

查找下一个兄弟使用next_sibling,查找上一个兄弟节点使用previous_sibling,如果是查找所有的,那么在对应的函数后面加s就可以

如何遍历树?

 使用find_all 函数

find_all(nameattrsrecursivetextlimit**kwargs)

举例说明:

print soup.find_all(‘title‘)
print soup.find_all(‘p‘,‘title‘)
print soup.find_all(‘a‘)
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值为:

[<title>The Dormouse‘s story</title>]
[<p class="title"><b>The Dormouse‘s story</b></p>]
[<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>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
[<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>]

通过css查找,直接上例子把:

print soup.find_all("a", class_="sister")
print soup.select("p.title")

通过属性进行查找
print soup.find_all("a", attrs={"class": "sister"})

通过文本进行查找
print soup.find_all(text="Elsie")
print soup.find_all(text=["Tillie", "Elsie", "Lacie"])

限制结果个数
print soup.find_all("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>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<p class="title"><b>The Dormouse‘s story</b></p>]
[<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>]
[u‘Elsie‘]
[u‘Elsie‘, u‘Lacie‘, u‘Tillie‘]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

总之,通过这些函数可以查找到想要的东西。

---end---

时间: 2024-07-30 10:56:34

Beautiful Soup第三方爬虫插件的相关文章

Python爬虫利器二之Beautiful Soup的用法

上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫Beautiful Soup,有了它我们可以很方便地提取出HTML或XML标签中的内容,实在是方便,这一节就让我们一起来感受一下Beautiful Soup的强大吧. 1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官

2017.08.11 Python网络爬虫实战之Beautiful Soup爬虫

1.与Scrapy不同的是Beautiful Soup并不是一个框架,而是一个模块:与Scrapy相比,bs4中间多了一道解析的过程(Scrapy是URL返回什么数据,程序就接受什么数据进行过滤),bs4则在接收数据和进行过滤之间多了一个解析的过程,根据解析器的不同,最终处理的数据也有所不同,加上这一步骤的优点是可以根据输入数据的不同进行针对性的解析:同一选择lxml解析器: 2.安装Beautiful Soup环境:pip install beautifulsoup4 3.Beautiful

【Python爬虫学习笔记(2)】Beautiful Soup库相关知识点总结

1. Beautiful Soup简介     Beautiful Soup是将数据从HTML和XML文件中解析出来的一个python库,它能够提供一种符合习惯的方法去遍历搜索和修改解析树,这将大大减少爬虫程序的运行时间.     Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码.你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautiful Soup就不能自动识别编码方式了.然后,你仅仅需要说明一下原始编码方式就可以了.     B

Python3网络爬虫(七):使用Beautiful Soup爬取小说

转载请注明作者和出处:http://blog.csdn.net/c406495762 运行平台: Windows Python版本: Python3.x IDE: Sublime text3 一.Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简

(转)python下很帅气的爬虫包 - Beautiful Soup 示例

官方文档地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html Beautiful Soup 相比其他的html解析有个非常重要的优势.html会被拆解为对象处理.全篇转化为字典和数组. 相比正则解析的爬虫,省略了学习正则的高成本. 相比xpath爬虫的解析,同样节约学习时间成本.虽然xpath已经简单点了.(爬虫框架Scrapy就是使用xpath) 安装 linux下可以执行 [plain] view plai

【爬虫】beautiful soup笔记(待填坑)

Beautiful Soup是一个第三方的网页解析的模块.其遵循的接口为Document Tree,将网页解析成为一个树形结构. 其使用步骤如下: 1.创建对象:根据网页的文档字符串 2.搜索节点:名称.属性.文字. 3.处理节点: BeautifulSoup(文档字符串, 'html.parser' 解析器,from_encoding='utf8') find_all(名称,属性,文字):可以传入字符串 也可以传入正则表达式. node.name 名称 node['href'] 属性 node

python爬虫之解析库Beautiful Soup

Beautiful Soup4操作 为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个标签的形式,来进行查找的,有点像jquery的形式.提升效率,我们在进行爬虫开发的时候,进程会用到正则来进行查找过滤的操作,纯手动会及其浪费时间. Beautiful Soup示例摘自官网 html_doc = """ <html>

【Python爬虫学习实践】基于Beautiful Soup的网站解析及数据可视化

在上一次的学习实践中,我们以Tencent职位信息网站为例,介绍了在爬虫中如何分析待解析的网站结构,同时也说明了利用Xpath和lxml解析网站的一般化流程.在本节的实践中,我们将以中国天气网为例,并基于Beautiful Soup库对其进行数据解析,最后再简单说明pyecharts数据可视化. 中国天气网网址:http://www.weather.com.cn/textFC/hb.shtml 和之前的Tencent职位信息实践一样,我们先来分析一下我们所爬取的网站的结构.在中国天气网中,我们可

Python 爬虫 解析库的使用 --- Beautiful Soup

知道了正则表达式的相关用法,但是一旦正则表达式写的有问题,得到的可能就不是我们想要的结果了.而且对于一个网页来说,都有一定的特殊结构和层级关系,而且有很多节点都有id或class来做区分,所以借助它们的结构和属性来提取也可以. 本随笔内容就来介绍一个强大的解析工作Beautiful Soup,它借助网页的结构和属性等特性来解析网页.有了它,我们不用再去写一些复杂的正则表达式,只需要简单的几条语句,就可以完成网页中某个元素的提取. 1.简介 简单来说,Beautiful Soup 就是Pyhon