BeautifulSoup 安装使用
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 安装使用的更多相关文章
- Python - BeautifulSoup 安装
BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...
- beautifulSoup安装
Python2.7 + beautifulSoup 4.4.1 安装配置 原创 2016年05月09日 10:20:30 标签: python 1261 1. 前言 最近研究python 的爬虫功能, ...
- 【Python实例二】之前期准备:Windows下的BeautifulSoup安装
前言 一直久闻Python的爬虫很高效,而且操作便捷,因此决定开始练习爬虫的相关内容. 首先尝试的是Python的爬虫利器之一:BeautifulSoup.(这名字听起来就有种想要去探究的兴趣.... ...
- [Python]BeautifulSoup安装与使用
1.BeautifulSoup简介 BeautifulSoup4和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据 ...
- beautifulsoup 安装
pip install beautifulsoup4
- Python网络爬虫与信息提取(二)—— BeautifulSoup
BeautifulSoup官方介绍: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. 官方 ...
- 解析库-beautifulsoup模块
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup # 安装:pip install beautifulsoup4 # Beautiful So ...
- Python HTML解析器BeautifulSoup(爬虫解析器)
BeautifulSoup简介 我们知道,Python拥有出色的内置HTML解析器模块——HTMLParser,然而还有一个功能更为强大的HTML或XML解析工具——BeautifulSoup(美味的 ...
- python学习(25) BeautifulSoup介绍和实战
BeautifulSoup是python的html解析库,处理html非常方便 BeautifulSoup 安装 pip install beautifulsoup4 BeautifulSoup 配合 ...
随机推荐
- UVA 10078 The Art Gallery
Problem: Century Arts has hundreds of art galleries scattered all around the country and you are hir ...
- Java内存模型(JMM)
参考: 1. http://www.tuicool.com/articles/UVzuQb
- volatile关键字的使用
(简要概括:volatile变量有两个作用:一个是告诉编译器不要进行优化:另一个是告诉系统始终从内存中取变量的地址,而不是从缓存中取变量的值) 一.前言 1.编译器优化介绍: 由于内存访问速度远不及C ...
- MySQL 操作表命令
新建表: create table Itemcats (id int(11) not null auto_increment, primary key (id)) engine=MyISAM auto ...
- 提高Python运行效率的六个窍门
曾灵敏 - MAY 18, 2015 Python是一门优秀的语言,它能让你在短时间内通过极少量代码就能完成许多操作.不仅如此,它还轻松支持多任务处理,比如多进程. 不喜欢Python的人经常会吐嘈P ...
- 服务接口API限流 Rate Limit
一.场景描述 很多做服务接口的人或多或少的遇到这样的场景,由于业务应用系统的负载能力有限,为了防止非预期的请求对系统压力过大而拖垮业务应用系统. 也就是面对大流量时,如何进行流量控制? 服务接口的流量 ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- sparksql链接mysql
1.在IDEA上建立一个sparksql_mysql的scala对象. 2.连接mysql的代码如下 import java.sql.{DriverManager, PreparedStatement ...
- 树莓派/RaspberryPi 内核编译
1.获取所需源码 1)下载地址: 官方网址:https://github.com/raspberrypi 上面列出了树莓派所有的开源软件: linux:内核源码 tools:编译内核和其他源码所需的工 ...
- POJ 1870 Bee Breeding(找规律)
题目链接 题意 : 给你一个蜂巢状图形,让你找出两个点之间的距离. 思路 : 在做这个题之前可以看一下2265,因为是一种题来着,规律就是我在2265里写的那样,然后就是求距离了,求距离的时候只需考虑 ...