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. error while loading shared libraries: libevent-2.0.so.5解决办法

    安装memcache时,需要建立文件索引或者说文件连接(link),类似windows下的快捷方式 启动服务时出现 error while loading shared libraries: libe ...

  2. 【周期串】NYOJ-1121 周期串

    [题目链接:NYOJ-1121] 例如:abcabcabc 该字符串的长度为9,那么周期串的长度len只可能为{1,3,9},否则就不可能构成周期串. 接下来,就是要在各周期间进行比较.描述不清... ...

  3. MVC ActionResult -- JavaScriptResult,JsonResult

    以下是ActionResult的继承图: 大概的分类: EmptyResult:表示不执行任何操作的结果 ContentResult :返回文本结果 JavaScriptResult:返回结果为Jav ...

  4. SQL Server 2012安装时如何不安装自带的Visual Studio

    不安装以下两个:

  5. [Everyday Mathematics]20150209

    设 $f$ 在区间 $I$ 上三阶可导, $f'\neq 0$, 则可定义 $f$ 的 Schwarz 导数: $$\bex S(f,x)=\frac{f'''(x)}{f'(x)}-\frac{3} ...

  6. 【LR】录制测试脚本中的基本菜单

    学习来源: MBoo,小强老师性能测试及Loadrunner培训  ——录制测试脚本: 1.Vuser -> run-time settings ->General Run Logic : ...

  7. C ~ 一个串口接收思路

    void uart_rx_isr(void) //接收中断函数 { uchar c; c=SBUF;//c等于接收的字节: switch (recv_state) { : if (c==0x02) / ...

  8. 游戏设计模式:Subclass Sandbox模式,以及功能方法集的设计思考

    书中总结出这种 Subclass Sandbox 的设计模式 Game Design Patterns: Subclass Sandbox 这种模式要点有两点: 在基类中实现各种功能性方法供子类调用 ...

  9. nginx+tomcat反向代理下使用tomcat-redis-session-manager进行session共享中值得注意的一个问题

    公司目前项目使用nginx反向代理+多个tomcat进行负载均衡,之前使用ip_hash策略进行session控制.近期有考虑不再使用ip_hash策略,所以需要进行session共享. 根据项目实际 ...

  10. 【转载】epoll的使用

    select,poll,epoll简介 select select本质上是通过设置或者检查存放fd标志位的数据结构来进行下一步处理.这样所带来的缺点是: 1 单个进程可监视的fd数量被限制 2 需要维 ...