BeautifulSoup4的安装及使用

简介:

一、BeautifulSoup4的安装
   方法一:cmd->easy_install BeautifulSoup
   方法二:从http://www.crummy.com/software/BeautifulSoup/bs4/download/
下载->cmd->进入下载的文件目录->python setuyp.py install

二、 BeautifulSoup4的使用 
 1、导入
    from bs4 import BeautifulSoup
    注意:要是BeautifulSoup的版本为3.x,则导入方式为:from BeautifulSoup import BeautifulSoup
 2、example
    html文件:
    html_doc = """

 The Dormouse's story

  Once upon a time there were three little sisters; and their names were ElsieLacie and Tillie; and they lived at the bottom of a well.

...

"""

 代码:
 from bs4 import BeautifulSoup
 soup = BeautifulSoup(html_doc)
 
 接下来可以开始使用各种功能

  soup.X (X为任意标签,返回整个标签,包括标签的属性,内容等)

 如:soup.title

   #

   soup.p

   #

 The Dormouse's story

  soup.a (注:仅仅返回第一个结果)

   # Elsie

   soup.find_all('a') (find_all 可以返回所有)

   # [Elsie,

   # Lacie,

   # Tillie]

   find还可以按属性查找
   soup.find(id="link3")
   # Tillie

   要取某个标签的某个属性,可用函数有 find_all,get
   for link in soup.find_all('a'):
     print(link.get('href'))
   # http://example.com/elsie
   # http://example.com/lacie
   # http://example.com/tillie

   要取html文件中的所有文本,可使用get_text()
   print(soup.get_text())
   # 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.
   # ...

   如果是打开html文件,语句可用:
   soup = BeautifulSoup(open("index.html"))
   BeautifulSoup中的Object
  tag (对应html中的标签)
   tag.attrs (以字典形式返回tag的所有属性)
  可以直接对tag的属性进行增、删、改,跟操作字典一样

   tag['class'] = 'verybold'

   tag['id'] = 1

   tag

   #< blockquote class="verybold" id="1">Extremely bold</blockquote>


   del tag['class']

   del tag['id']

   tag

   #< blockquote>Extremely bold</blockquote>

   tag['class']

   # KeyError: 'class'

   print(tag.get('class'))

   # None


   X.contents (X为标签,可返回标签的内容)

   eg.

   head_tag = soup.head

   head_tag

   #< head><title>The Dormouse's story</title></head>

   head_tag.contents

   [<title>The Dormouse's story</title>]

   title_tag = head_tag.contents[0]

   title_tag

   #< title>The Dormouse's story</title>

   title_tag.contents

   # [u'The Dormouse's story']


   解决解析网页出现乱码问题:
   import urllib2
   2    from BeautifulSoup import BeautifulSoup
   3    
   4    page = urllib2.urlopen('http://www.leeon.me');
   5    soup = BeautifulSoup(page,fromEncoding="gb18030")
   6    
   7    print soup.originalEncoding
   8    print soup.prettify()



#

# python --version
# wget https://www.crummy.com/software/BeautifulSoup/bs4/download/4.6/beautifulsoup4-4.6.0.tar.gz
# tar -zxvf beautifulsoup4-4.6.0.tar.gz
# cd beautifulsoup4-4.6.0/
# python setup.py install



本文转自 guowang327 51CTO博客,原文链接:http://blog.51cto.com/guowang327/1927398,如需转载请自行联系原作者

相关文章
|
4天前
|
XML 数据格式
Beautiful Soup 库提供了许多常用的方法
Beautiful Soup库用于HTML/XML文档解析和操作,提供初始化、查找、提取信息及修改文档的方法。如:find()和find_all()查找元素,.string或.get_text()获取文本,.attrs获取属性,.append()、.insert()、.remove()、.replace_with()、.unwrap()和.wrap()修改文档结构。还有.prettify()格式化输出,.encode()和.decode()处理编码。这些功能组合使用可灵活处理文档信息。
8 1
|
7天前
|
数据采集 Web App开发 安全
Beautiful Soup和Requests
【5月更文挑战第7天】本文介绍了使用Python中的Requests和Beautiful Soup库创建网络爬虫的方法。Requests库简化了HTTP请求,Beautiful Soup则用于解析HTML和XML文档,便于提取信息。首先,文章解释了两个库的作用和安装步骤。接着,通过实例展示了如何提取网页标题和链接,以及如何下载并保存图片。对于动态加载的内容,文章推荐使用Selenium库模拟浏览器行为。此外,还介绍了如何处理登录认证,包括安全输入密码和从外部文件读取凭据。总结来说,本文提供了Python网络爬虫的基础知识和实用技巧。
23 6
|
5月前
|
XML API 数据格式
Beautiful Soup
Beautiful Soup 是一个用于从网页中提取数据的 Python 库。它可以帮助用户轻松地解析 HTML 和 XML 文档,并从中提取所需的信息。Beautiful Soup 基于 Python 的标准库,因此无需安装任何额外的依赖包即可使用。
48 7
|
XML 数据格式 Python
Beauiful Soup
Beautiful Soup的简单使用
|
Python
Beautiful Soup库的介绍
本节中将介绍如何使用 Beautiful Soup 来解析 HTML 以获取我们想要的信息。
78 0
|
XML 数据格式 Python
每日一模块——BeautifulSoup4
每日一模块——BeautifulSoup4
|
Python 数据采集 编译器