BeautifulSoup模块是干嘛的?

答:通过html标签去快速匹配标签中的内容。效率相对比正则会好的多。效率跟xpath模块应该差不多。

一:解析器:

  • BeautifulSoup(html,"html.parser")
  • BeautifulSoup(html,'lxml')
  • BeautifulSoup(html,'xml')
  • BeautifulSoup(html,'html5lib')

假设要匹配a标签里的href属性:

 html = "<a href='http://baidu.com/'>this is baidu.com</a>"
bs = BeautifulSoup(html,"lxml")
all_href = bs.find_all('a')
for i in all_href:
print i['href']
 #!usr/bin/env python
#encding:utf-8
#by i3ekr import requests
from bs4 import BeautifulSoup html = """
<!DOCTYPE html>
<html>
<head>
<title>title test demo</title>
</head>
<body>
<h1>this is h1</h1>
<h1>this is h1 two</h1>
<h1>this is h1 stree</h1>
<a href="http://baidu.com">this is a href.</a>
</body>
</html>
"""
bs = BeautifulSoup(html, "lxml")
print bs.find_all('h1')

python中BeautifulSoup模块的更多相关文章

  1. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

  2. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  3. 【转】关于python中re模块split方法的使用

    注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...

  4. Python中的模块介绍和使用

    在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...

  5. python中导入模块的本质, 无法导入手写模块的解决办法

    最近身边一些朋友发生在项目当中编写自己模块,导入的时候无法导入的问题. 下面我来分享一下关于python中导入模块的一些基本知识. 1 导入模块时寻找路径 在每一个运行的python程序当中,都维护了 ...

  6. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  7. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  8. Python中pathlib模块

    Python中pathlib模块 Path.cwd():返回当前目录的路径 Path.home():返回当前用户的家目录 Path.stat():返回此路径信息 Path.touch():创建文件 P ...

  9. Python 中包/模块的 `import` 操作

    版权声明:博客为作者原创,允许转载,但必须注明原文地址: https://www.cnblogs.com/byronxie/p/10745292.html 用实例来说明 import 的作用吧. 创建 ...

随机推荐

  1. 在windows搭建react

    1.安装必须的软件 1.Python 2    注意勾选 Add python.exe to Path,选项,这样就可以在安装完成后,不用手动去添加环境变量    安装完,打开cmd.exe,输入py ...

  2. Calendar简单用法

  3. TScreen 类 - 通过 Screen 更换光标

    //更换窗体或某个控件的光标可以不通过 Screen 对象, 譬如: begin   Self.Cursor := crAppStart;   Panel1.Cursor := crHandPoint ...

  4. 新版chrome touch警告处理办法

    最近做项目经常在 chrome 的控制台看到如下提示: Unable to preventDefault inside passive event listener due to target bei ...

  5. BZOJ4003:[JLOI2015]城池攻占——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 https://www.luogu.org/problemnew/show/P3261 小铭 ...

  6. BZOJ1057:[ZJOI2007]棋盘制作——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1057 https://www.luogu.org/problemnew/show/P1169 国际象 ...

  7. BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】

    题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...

  8. 关于javascript数组的定义与其一些常用方法总结

    由于JavaScript是一门宽松的语言,这种宽松可能会带来更加麻烦的事情.比如JavaScript的数组,定义与使用的方式太灵活有时候让人迷惑.下面将JavaScript中关于数组常用的方法.定义之 ...

  9. git安装配置和使用

    ## 安装git服务器 ## 安装git sudo apt-get install git ## 建立git用户 sudo adduser git ## 修改git用户 * 设置不能登录 vim /e ...

  10. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...