好久没更新博客了。打算写一个python的爬虫系列及数据分析。falg也不能随便立,以免打脸。

python爬取内容,是过程,分析数据是结果,最终得出结论才是目的。python爬虫爬取了内容,一般都是从网页上获取,那我们从html页面中如何提取出自己想要的信息呢?那就需要解析。目前常用的有BeautifulSoup、PyQuery、XPath和正则表达式。正则容易出错,而且一直是弱项,就讲讲其他三个的使用,今天先看下BeautifulSoup.

一、简介

BeautifulSoup直译为美丽的汤。是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。

二、安装

 pip install beautifulsoup4

三、准备测试代码

这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档)

<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>
</body>
</html>

我们先以上述代码为例进行测试

四、使用

from bs4 import BeautifulSoup

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>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, features="html.parser")
#print(soup.prettify()) print(soup.title)
#<title>The Dormouse's story</title>
print(soup.title.name)
#title
print(soup.title.string)
#The Dormouse's story
print(soup.title.parent.name)
#head print(soup.p)
#<p class="title"><b>The Dormouse's story</b></p>
print(soup.p['class'])
#[u'title'] print(soup.a)
#<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(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>]
print(soup.find(id='link3'))
#<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> for link in soup.find_all('a'):
print(link.get('href'))
#http://example.com/elsie
#http://example.com/lacie
#http://example.com/tillie print(soup.get_text())
#The Dormouse's story #The Dormouse's story
#Once upon a time there were three little sisters; and their names were
#Elsie,
#Lacie and
#Tillie;
#and they lived at the bottom of a well.
#...

以上注释的都是上一行输出的

五、BeautifulSoup可以传入字符串或文件句柄

from bs4 import BeautifulSoup

soup = BeautifulSoup('<b class="boldest">Extremely bold</b>', features="lxml")
tag = soup.b
print(tag)
#<b class="boldest">Extremely bold</b>
tag.name = "blockquote"
print(tag)
#<blockquote class="boldest">Extremely bold</blockquote>
print(tag['class'])
#['boldest']
print(tag.attrs)
#{'class': ['boldest']}
tag['id']="stylebs"
print(tag)
#<blockquote class="boldest" id="stylebs">Extremely bold</blockquote>
del tag['id']
print(tag)
#<blockquote class="boldest">Extremely bold</blockquote> css_soup = BeautifulSoup('<p class="body strikeout"></p>', features="lxml")
print(css_soup.p['class'])
#['body', 'strikeout'] id_soup = BeautifulSoup('<p id="my id"></p>', features="lxml")
print(id_soup.p['id'])
#my id rel_soup = BeautifulSoup('<p>Back to the <a rel="index">homepage</a></p>', features="lxml")
print(rel_soup.a['rel'])
#['index']
rel_soup.a['rel'] = ['index', 'contents']
print(rel_soup.p)

参考文档 : https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#id40

python系列之(1)BeautifulSoup的用法的更多相关文章

  1. 孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3

    孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步了 ...

  2. 孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2

    孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步 ...

  3. Python爬虫之BeautifulSoup的用法

    之前看静觅博客,关于BeautifulSoup的用法不太熟练,所以趁机在网上搜索相关的视频,其中一个讲的还是挺清楚的:python爬虫小白入门之BeautifulSoup库,有空做了一下笔记: 一.爬 ...

  4. 总结整理 -- python系列

    python系列 python--基础学习(一)开发环境搭建,体验HelloWorld python--基础学习(二)判断 .循环.定义函数.继承.调用 python--基础学习(三)字符串单引号.双 ...

  5. 初探接口测试框架--python系列7

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  6. 初探接口测试框架--python系列2

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  7. 初探接口测试框架--python系列3

    点击标题下「微信」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...

  8. 初探接口测试框架--python系列4

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  9. 初探接口测试框架--python系列5

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

  10. 初探接口测试框架--python系列6

    点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...

随机推荐

  1. javascript date utc

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangbin10025/article/details/37503465 近期在使用highsto ...

  2. 二、Web Service开发(.net)

    .net平台内建了对Web Service的支持,包括Web Service的构建和使用.与其它开发平台不同,使用.net平台,你不需要其他的工具或者SDK就可以完成Web Service的开发了.. ...

  3. 粗浅看 Tomcat系统架构分析

    原文出处: 吴士龙 http://www.importnew.com/21112.html Tomcat的结构很复杂,但是Tomcat也非常的模块化,找到了Tomcat最核心的模块,就抓住了Tomca ...

  4. springcloud:RPC和HTTP

    1.RPC和HTTP 无论是微服务还是SOA,都面临着服务间的远程调用.那么服务间的远程调用方式有哪些呢? 常见的远程调用方式有以下2种: RPC:Remote Produce Call远程过程调用, ...

  5. IntersectionObserver API 使用教程(转载)

    作者: 阮一峰 日期: 2016年11月 3日 网页开发时,常常需要了解某个元素是否进入了"视口"(viewport),即用户能不能看到它. 上图的绿色方块不断滚动,顶部会提示它的 ...

  6. UVA11389 The Bus Driver Problem

        题意:有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d*r.问如何分配路线才能使加班费最少.   贪心 ...

  7. CSS3的transform 转换

    transform是可以实现元素位移,旋转,缩放和变形.只介绍了2D转换~ translate 位移:改变元素位置 最多设置两个值,一个水平,一个垂直.如果设置为负数,则代表反方向.可设置百分比.eg ...

  8. []==![] 为什么等于true?

    最近碰到这样一个问题: []==![]  为什么等于true? 首先分析  !的优先级较==高,先运算==两侧的操作数: typeof []; //"object" typeof ...

  9. java知识点---文件分隔符

    本篇讲述java编程中,怎样解决跨平台时,因不同系统中分隔符不同导致的文件或路径找不到的问题 首先来看两个例子: 一.linux系统和windows系统中的文件路径: Linux系统: Windows ...

  10. jsp页面el表达式<c:choose> <c:when的用法

    等于 是if else <c:choose> <c:when test="${paginationModel.py_province != ''}"> 如果 ...