BeautifulSoup中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

BeautifulSoup下载:http://www.crummy.com/software/BeautifulSoup/

解压到任意目录

在cmd控制台下进入目录

执行:python setup.py install即可;

执行完后命令行进入python使用import bs4命令验证是否成功:

假设content变量里存着整个网页的字符串,或者是urllib.request.urlopen(url)的返回值

首先,导入模块,然后把content打包进soup里

from bs4 import BeautifulSoup
soup = BeautifulSoup(content,'html.parser')

1.将字符串以网页的形式美化显示(返回的是一个字符串)

print(soup.prettify())

2.提取出网页中的特定标签

  比如:提取出所有<a>标签

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all('a'))

  或者提取出所有<a>标签和<b>标签

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(['a','b']))

  或者提取出所有class为t-large的<span>标签(也就是所有类似于<span class="t-large"></span>的标签)

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all('span','t-large'))

  或者提取出所有有class属性没有id属性的标签

def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(has_class_but_no_id))

  或者提取出所有id等于"link2"的标签

soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(id="link2"))

3.获取一个标签(一个soup对象)的内容.contents

print(soup.contents)
print(soup.a.contents)

4.获取一个标签的class属性(要特别注意返回的是list,哪怕只有一个元素,因为HTML新特性——多属性导致的)

print(soup.a['class'])

5.删除一个标签

>>> soup = BeautifulSoup('<script>a</script>baba<script>b</script>')
>>> [s.extract() for s in soup('script')]
>>> soup
baba

6.删除一个特定class的标签

from bs4 import BeautifulSoup

markup = '<a>This is not div <div class="1">This is div 1</div><div class="2">This is div 2</div></a>'
soup = BeautifulSoup(markup,"html.parser")
a_tag = soup soup.find('div',class_='').decompose() print a_tag #<a>This is not div <div class="1">This is div 1</div></a>

7.注意在beautifulsoup中,<br>标签写成<br/>

8.提取一个soup里的所有字符串

for string in soup.strings:
print(repr(string))

提取一个soup里的非空行非空白字符串

for string in soup.stripped_strings:
print(repr(string))

Python beautifulsoup模块的更多相关文章

  1. python BeautifulSoup模块的简要介绍

    常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...

  2. Python 爬虫三 beautifulsoup模块

    beautifulsoup模块 BeautifulSoup模块 BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查 ...

  3. python中BeautifulSoup模块

    BeautifulSoup模块是干嘛的? 答:通过html标签去快速匹配标签中的内容.效率相对比正则会好的多.效率跟xpath模块应该差不多. 一:解析器: BeautifulSoup(html,&q ...

  4. 孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1

    孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1 (完整学习过程屏幕记录视频地址在文末) 感觉用requests获取到网页的html源代码后,更重要的工作其实是分析得到的内 ...

  5. Python爬虫之Beautifulsoup模块的使用

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

  6. python学习之BeautifulSoup模块爬图

    BeautifulSoup模块爬图学习HTML文本解析标签定位网上教程多是爬mzitu,此网站反爬限制多了.随意找了个网址,解析速度有些慢.脚本流程:首页获取总页数-->拼接每页URL--> ...

  7. python 常用模块(转载)

    转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...

  8. Python - BeautifulSoup 安装

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

  9. 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用

    [爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...

随机推荐

  1. cocos2d-x中的Jni使用(C++与Andriod方法互调)

    作者:何卫 转载请注明,原文链接:http://www.cnblogs.com/hewei2012/p/3376616.html 前提条件: 1.操作的游戏工程和cocos2d_x游戏引擎是一个目录的 ...

  2. vim 支持 markdown 语法

    /************************************************************************* * vim 支持 markdown 语法 * 说明 ...

  3. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  4. Android下二维码的扫描

    Android平台下 二维码的扫描一般采用: Zxing:参考地址 Zxing功能比较强大,支持条形码和二维码的扫描,用的人也比较多,但是Zxing太大,一般开发简单的app,用起来比较麻烦. 所以网 ...

  5. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  6. jsp防盗链代码

    // 禁止缓存   response.setHeader("Cache-Control", "no-store");   response.setHeader( ...

  7. MySQL 5.6 复制:GTID 的优点和限制(第一部分)

    全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性.它为维护特定的复制拓扑结构下服务器的DBA们大幅度改善他们的工作状况提供了多种可能性 ...

  8. linux 下 NetBeans 字体大小设置

    在linux mint 12下安装了 NetBeans7.1.2使用之后,觉得字体不好看,字体普遍特别大,分三个方面改NetBeans的字体. 1. 代码字体大小 点击NetBeans菜单,工具--& ...

  9. cadence异型孔

    1:画route path的边框,画在board geometry的ncroute_path层上,可以用zcopy (暂时没用过)      没有的话可能:WARNING: No route path ...

  10. 【数据结构和算法】 O(1)时间取得栈中的最大 / 最小元素值

    常数时间取得栈中的元素最大值和最小值,我们可以想到当push的时候比较一下,如果待push元素值小于栈顶元素,则更新min值,最大值亦然. 这样有个问题就是当pop的时候,就没了最大最小值. 于是上网 ...