python系列之(1)BeautifulSoup的用法
好久没更新博客了。打算写一个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的用法的更多相关文章
- 孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3
孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步了 ...
- 孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2
孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步 ...
- Python爬虫之BeautifulSoup的用法
之前看静觅博客,关于BeautifulSoup的用法不太熟练,所以趁机在网上搜索相关的视频,其中一个讲的还是挺清楚的:python爬虫小白入门之BeautifulSoup库,有空做了一下笔记: 一.爬 ...
- 总结整理 -- python系列
python系列 python--基础学习(一)开发环境搭建,体验HelloWorld python--基础学习(二)判断 .循环.定义函数.继承.调用 python--基础学习(三)字符串单引号.双 ...
- 初探接口测试框架--python系列7
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 初探接口测试框架--python系列2
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 初探接口测试框架--python系列3
点击标题下「微信」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...
- 初探接口测试框架--python系列4
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 初探接口测试框架--python系列5
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 初探接口测试框架--python系列6
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
随机推荐
- Linux下ps -ef和ps aux的区别及格式详解-转
原文:https://www.linuxidc.com/Linux/2016-07/133515.htm Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区 ...
- UVa-401 Palindromes回文词
虽然是水题,但是容易错.参照了紫书的代码可以写的很简洁.主要还是注意常量数组的使用,能让代码变得简单许多 #include <iostream> #include <cstdio&g ...
- 【python之路42】web框架们的具体用法
Python的WEB框架 (一).Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. p ...
- 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)
本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...
- 傳説中的 jsonp
jsonp的由來 1 . 網頁上的東西衹要跨域了就不能傳送或者接受數據了.不管是什麽衹要是跨域.Ajax直接请求普通文件存在跨域无权限访问的问题, 2 . 但是src這個東西比較厲害了,請求哪裏都可以 ...
- 【JZOJ5231】【NOIP2017模拟A组模拟8.5】序列问题 线段树
题面 100 在\(O(n^2)\)的基础上,我们可以用线段树来加速. 枚举了左端点之后,需要知道以这个左端点为起点的前缀max,前缀min. 这里只讨论前缀max,前缀min同理. 当我们倒序枚举左 ...
- Mac+Webstorm 双更新后 webstorm无法使用内置svn
我终于营业了!!!!!! EachTime!!!! 我更新了mac系统后,就会莫名其妙的webstorm的svn无法使用 具体表现为无法更新和提交 具体报错为:Can't use Subversion ...
- java swing调试时线程显示名字
一般有一个默认名字 但是具体运行到哪一个线程,需要猜 为了节约时间,提高效率 可以给线程写个中文名(因为默认就是英文,写中文,一眼就能挑出来) 以RTC定时器为例子 final TimerRtc ti ...
- <a>标签操作
1.点击后onclick事件失效,变灰,不可用 onclick(this); //事件传递this对象 function viewMm(obj) { $(obj).removeAttr("o ...
- 2019-10-18-WPF-解决-StylusPlugIn-点击穿透问题
title author date CreateTime categories WPF 解决 StylusPlugIn 点击穿透问题 lindexi 2019-10-18 20:55:35 +0800 ...