Linux环境

1. 安装

方法一:

下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/4.2/

解压:tar -xzvf beautifulsoup4-4.2.0.tar.gz

安装:进入解压后的目录

python setup.py build
sudo python setup.py install

方法二(快速安装)

(Ubuntu) sudo apt-get install python-bs4
或者
install beautifulsoup4
或着
easy_install beautifulsoup4

2. 引用(python环境下)

from bs4 import BeautifulSoup

3. 使用

案例

html_doc = """
<html><head><title>The Dormouse's story</title></head> <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>
"""

开始

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
>>> soup.head()
[<title>The Dormouse's story</title>]
>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.string
u"The Dormouse's story"
>>> soup.body.b
<b>The Dormouse's story</b>
>>> soup.body.b.string
u"The Dormouse's story"
>>> soup.a
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

找到所有的a

soup.find_all('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中的信息

>>> for key in soup.find_all('a'):
... print key.get('class'), key.get("href")
...
['sister'] http://example.com/elsie
['sister'] http://example.com/lacie
['sister'] http://example.com/tillie

参考

http://www.crummy.com/software/BeautifulSoup/bs4/doc/

BeautifulSoup 安装使用的更多相关文章

  1. Python - BeautifulSoup 安装

    BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...

  2. beautifulSoup安装

    Python2.7 + beautifulSoup 4.4.1 安装配置 原创 2016年05月09日 10:20:30 标签: python 1261 1. 前言 最近研究python 的爬虫功能, ...

  3. 【Python实例二】之前期准备:Windows下的BeautifulSoup安装

    前言 一直久闻Python的爬虫很高效,而且操作便捷,因此决定开始练习爬虫的相关内容. 首先尝试的是Python的爬虫利器之一:BeautifulSoup.(这名字听起来就有种想要去探究的兴趣.... ...

  4. [Python]BeautifulSoup安装与使用

    1.BeautifulSoup简介 BeautifulSoup4和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据 ...

  5. beautifulsoup 安装

    pip install beautifulsoup4

  6. Python网络爬虫与信息提取(二)—— BeautifulSoup

    BeautifulSoup官方介绍: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. 官方 ...

  7. 解析库-beautifulsoup模块

    # -*- coding: utf-8 -*- from bs4 import BeautifulSoup # 安装:pip install beautifulsoup4 # Beautiful So ...

  8. Python HTML解析器BeautifulSoup(爬虫解析器)

    BeautifulSoup简介 我们知道,Python拥有出色的内置HTML解析器模块——HTMLParser,然而还有一个功能更为强大的HTML或XML解析工具——BeautifulSoup(美味的 ...

  9. python学习(25) BeautifulSoup介绍和实战

    BeautifulSoup是python的html解析库,处理html非常方便 BeautifulSoup 安装 pip install beautifulsoup4 BeautifulSoup 配合 ...

随机推荐

  1. Netsharp快速入门(之8) 基础档案(工作区2 设置商品主列表、规格细列表、商品表单、查询)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 3.5.1.1 列表设置 1.选择第一行主列表,点工具-列表方案 2.打开列表方案界面后,在列表项目填入需要用到实体Demo.Arc ...

  2. Function模式 -- 深入理解javascript

    /* 一.回调函数 函数A作为另外一个函数B的参数 */ var menuId = $("ul.nav").first().attr("id"); var re ...

  3. 增强学习贪心算法与Softmax算法

    (一) 这个算法是基于一个概率来对探索和利用进行折中:每次尝试时,以概率进行探索,即以均匀概率随机选取一个摇臂,以的概率进行利用,即以这个概率选择当前平均奖赏最高的摇臂(如有多个,则随机选取). 其中 ...

  4. Ubuntu 16.04 下卸载 lnmp/lamp 方法

    1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...

  5. 使用CSS时间打点的Loading效果的教程

    基于box-shadow实现的打点效果 理论上,box-shadow可以实现任意的图形效果,自然我们可以利用box-shadow生成我们的点效果,然后通过animation控制不同时间点点的数目就可以 ...

  6. Matlab命令系列之目录操作

    Matlab命令系列之目录操作 filesep 用于返回当前平台的目录分隔符,Windows是反斜杠(),Linux是斜杠(/).有时此命令结合ispc命令使用,可以灵活的设置目录分割符. fullf ...

  7. HDU 1028Ignatius and the Princess III(母函数简单题)

     Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. ios 聊天demo 和nsoperationdemo

    http://blog.csdn.net/zhibudefeng/article/details/7991649 http://blog.csdn.net/kangx6/article/details ...

  9. UNITY_MATRIX_IT_MV[Matrix] (转载)

    转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...

  10. Unity3D战争迷雾效果

    原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...