喵吉欧尼酱

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

BeautifulSoup安装    

终端输入:  pip install BeautlfulSoup4

Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml 

 pip install lxml

另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:

pip install html5lib

Python标准库 BeautifulSoup(markup,"html.parser")
  • Python的内置标准库
  • 执行速度适中
  • 文档容错能力强
  • Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
lxml HTML 解析器 BeautifulSoup(markup, "lxml")
  • 速度快
  • 文档容错能力强
  • 需要安装C语言库
lxml XML 解析器

BeautifulSoup(markup, ["lxml","xml"])

BeautifulSoup(markup, "xml")

  • 速度快
  • 唯一支持XML的解析器
  • 需要安装C语言库
html5lib BeautifulSoup(markup, "html5lib")
  • 最好的容错性
  • 以浏览器的方式解析文档
  • 生成HTML5格式的文档
  • 速度慢
  • 不依赖外部扩展

=====================================================

模块导入 :

from bs4 import BeautifulSoup

======================================

模块的使用

from bs4 import BeautifulSoup
import requests
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,'html.parser')
#print(soup.a.name) #a
#print(soup.a.parent.name) #p
#print(soup.a.parent.parent.name)#body
#print(soup.title)#<title>The Dormouse's story</title>
#print(soup.title.string)#The Dormouse's story
#print(soup.p.string)#The Dormouse's story
#print(soup.p['class'])#['title']
#print(soup.find_all('a'))#打印出所有的  a  标签
#print(soup.find(id='link2'))#<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>

 

从文档中找到所有<a>标签的链接:

for i in soup.find_all('a'):
    print(i.get('href'))

 

posted on 2017-08-25 20:15  喵吉欧尼酱  阅读(109)  评论(0)    收藏  举报