好久没更新博客了。打算写一个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. Mycat+MySQL 主从复制

    一.主从复制搭建(新环境5.6.33)1.设置复制Master配置信息 [mysqld] #repl master库 server \logbin\mysql-bin max_binlog_size= ...

  2. C# 无边框窗体移动和改变大小的实现

    自己给软件做的皮肤,将窗体设为无边框后,想要其具有正常窗体的移动和改变大小功能,以下代 码可以实现.... //需添加using System.Runtime.InteropServices; [Dl ...

  3. mongodb 使用常见问题汇总(主要是集群搭建)

    1 安装 请使用官方推荐的教程 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ 2 远程访问失败: 操作系统是u ...

  4. jquery ajax跨越

    JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问 1.jsonp之$.ajax js $.ajax({ ...

  5. JavaScript中this的指向(转载)

    转载自:http://www.cnblogs.com/dongcanliang/p/7054176.html 前言 this 指向问题是入坑前端必须了解知识点,现在迎来了ES6时代,因为箭头函数的出现 ...

  6. 20190922-雅礼Day2

    先送大家几个变量名: 具体的可以去$C++ \ Reference$里看(本页 右侧/下侧 有链接) 或者等一下奇迹银桥第三氮 const int c; mutable int a; volatile ...

  7. CodeChef August Lunchtime 2014 题解

    A题 给一个由a和b两种类型的字符组成的字符串,每次可以从中选取任意长度的回文子序列(不一定连续)并删除.问最少需要几次能将整个字符串为空. 思路:如果本身是个回文串,那么只需要一次,否则需要两次(第 ...

  8. 从默认的index.jsp页面跳转或转发到其他页面

    使用forward还是redirect都可以完成跳转 forward:浏览器地址不变,所以存在重复提交的问题 <%  pageContext.forward("student/list ...

  9. 实时查看linux网卡流量 的base脚本

    #!/bin/bash " ] do eth=$ RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{pri ...

  10. python 处理缺失值