BeautifulSoup 库

https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#id4

中文版BeautifulSoup库

作用

  1. 提取HTML和XML文档中的数据
  2. 修改、导航、查找文档

创建html_doc

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

#使用bs4库
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html_doc)
>>> print soup.prettify()
<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 class="sister" href="http://example.com/elsie" id="link1">
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;  
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>

提取所需的字段
>>> soup.title                                                    #提取标题
<title>The Dormouse‘s story</title>
>>> soup.title.name
‘title‘

>>> soup.title.string                                            #提取标题的内容
u"The Dormouse‘s story"

>>> soup.a                                                        #提取<a>字段信息(第一个<a>)
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

>>> soup.p
<p class="title"><b>The Dormouse‘s story</b></p>
>>> soup.p[‘class‘] 
[‘title‘]

查找<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>]

>>> for link in soup.find_all(‘a‘):
...     print link.get(‘href‘)                        #提取link, href字段
... 
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

时间: 2024-08-24 03:43:06

BeautifulSoup 库的相关文章

解决BeautifulSoup库运行时报错问题

解决BeautifulSoup库运行时报错问题 运行BeautifulSoup库时可能出现下面的错误,具体错误消息为:To get rid of this warning, change this: BeautifulSoup([your markup]) to this: BeautifulSoup([your markup], "html.parser") 2.修改方法: 根据提示,将初始化,soup=BeautifulSoup(doc)修改为soup=BeautifulSoup(

Python爬虫利器:BeautifulSoup库

Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup库是解析.遍历.维护 "标签树" 的功能库(遍历,是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问).https://www.crummy.com/software/BeautifulSoup BeautifulSoup库我们常称之为bs4,导入该库为:from bs4 im

python下载安装BeautifulSoup库

python下载安装BeautifulSoup库 1.下载https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/ 2.解压到解压到python目录下: 3.“win+R”进入cmd:依次输入如下代码: C:\Users\Administrator>cd D:\softwareIT\Python27\beautifulsoup4-4.5.0 C:\Users\Administrator>d: D:\softwareIT\Py

BeautifulSoup库的安装与使用

BeautifulSoup库的安装 Win平台:"以管理员身份运行" cmd 执行 pip install beautifulsoup4 演示HTML页面地址:http://python123.io/ws//demo.html 文件名称:demo.html 网页源代码:HTML 5.0 格式代码 BeautifulSoup库的安装小测: >>> import requests >>> r = requests.get("http://pyt

网络爬虫BeautifulSoup库的使用

使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='http://python123.io/ws/demo.html' r=requests.get(url) if r.status_code==200: print('网络请求成功') demo=r.text soup=BeautifulSoup(demo,'html.parser') print(sou

BeautifulSoup库的简单实用

1.BeautifulSoup库的简单理解 打开一个简单的html文件(每一对尖括号形成一个标签,标签之间有上下之间的关系,形成了标签树) <html> <body> <p class="title">....</p> </body> </html> BeautifulSoup库是解析.遍历.维护“标签树”的功能库. 针对其中一个标签进行举例说明: 2.BeautifulSoup库的引用 最常用的是: from b

Python:使用 BeautifulSoup 库抓取百度天气

最近研究了Python的BeautifulSoup库,用起来还挺好玩的一.安装:使用pip命令在线安装:在cmd窗口中输入:pip install beautilfulsoup4 二.代码思路:1.使用request获取相关网页的返回值,即HTML对象: 方法一2.通过BeautifulSoup库对HTML页面元素进行解析,需要先分析要抓取的内容在哪里,再通过代码获取,存储在列表中:方法二3.读取列表中内容,写入到csv文件中.方法三 ```pythonfrom bs4 import Beaut

四 . 爬虫 BeautifulSoup库参数和使用

一  .BeautifulSoup库使用和参数 1 .Beautiful简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序.Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码.你

BeautifulSoup库的使用

一.beautifulSoup库的安装 pip install BeautifulSoup 二.beautifulsoup库的使用 1.调用beautifulsoup库 from bs4 import beautifulsoup4 import bs4 2.beautifulsoup简单使用 from bs4 import BeautifulSoup soup = BeautifulSoup("<html>data<html>","html.parse