好久没更新博客了。打算写一个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. WIN10 Internet协议版本tcp/ipv4属性灰色没有法点击

    问题图如下: 1.右键打开“计算机”点击菜单,选中“管理”: 2.进入计算机管理后,打开“设备管理器”,然后在右侧打开点击“网络适配器”,如果有驱动的话,在驱动上右键点击菜单,选中卸载: 3.卸载网卡 ...

  2. struts2-OGNL表达式-OGNL与Struts2的结合

    1 OGNL表达式 OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航.OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 1.1 使用OGNL 导包 ...

  3. Luogu P2864 [USACO06JAN]树林The Grove(bfs)

    P2864 [USACO06JAN]树林The Grove(bfs) 题面 题目描述 The pasture contains a small, contiguous grove of trees t ...

  4. goland设置颜色和字体

  5. rabbitmq实现单发送单接收

    1.创建两个项目.都使其支持rabbitmq (1)在pom.xml文件中添加支持rabbitmq的jar包 <dependency> <groupId>org.springf ...

  6. 【python之路25】正则表达式

    一.正则表达式简介 就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的(在python中),它内嵌在python中,并通过RE模块实现.正则表达式编译成一系列字节码,然后由用C编写的匹配引擎 ...

  7. 调用本地摄像头并通过canvas拍照

    首先我们需要新建一个video标签,并且放到html里边 var video = document.createElement("video"); video.autoplay=& ...

  8. VMWare 下 Ubuntu 18.04 的文件共享

    突然某天发现 /mnt/hgfs 下共享的文件夹没了... apt-get install open-vm-tools mkdir /mnt/hgfs vmhgfs-fuse .host:/ /mnt ...

  9. 解决驱动版本不对应导致的UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0

    org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is ...

  10. oracle使用dblink跨库查询的例子

    本文介绍了oracle数据库使用dblink进行跨库查询的方法,oracle dblink跨库查询教程,需要的朋友参考下.   oracle dblink跨库查询 方法一:首先,创建数据库链接: 复制 ...