读BeautifulSoup官方文档之html树的打印
prettify()能返回一个格式良好的html的Unicode字符串 :
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
soup.prettify()
# '<html>\n <head>\n </head>\n <body>\n <a href="http://example.com/">\n...' print(soup.prettify())
# <html>
# <head>
# </head>
# <body>
# <a href="http://example.com/">
# I linked to
# <i>
# example.com
# </i>
# </a>
# </body>
# </html>
但是你只是想要一个代表该html的字符串, 并不在乎它的格式, 你可以使用str()或者unicode()...这里str()返回的是格式为utf8的字符串, 你可以使用encode使它变为bytestring或者decode使它变成Unicode.
str(soup)
# '<html><head></head><body><a href="http://example.com/">I linked to <i>example.com</i></a></body></html>' unicode(soup.a)
# u'<a href="http://example.com/">I linked to <i>example.com</i></a>'
其他还有一些细节我不太像看下去了, 最后还有一个get_text()我在提下, 它能返回调用标签中所有的text部分...
markup = '<a href="http://example.com/">\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup) soup.get_text()
u'\nI linked to example.com\n'
soup.i.get_text()
u'example.com'
你还可以为他传递一个字符串参数, 用这个参数来划分出每一部分的text.
# soup.get_text("|")
u'\nI linked to |example.com|\n'
同时还可以设置strip参数来去掉每个部分(注意是每个部分而不是整体)前后的空白字符
# soup.get_text("|", strip=True)
u'I linked to|example.com'
当然, 这种情况也可以使用我们之前提到的stripped_strings(), 不记得的可以看之前的文章...
[text for text in soup.stripped_strings]
# [u'I linked to', u'example.com']
看到这里文档也看完了70%左右, 我感觉这些已经足够我目前的需求了, 所以就我不就继续往下看了...
读BeautifulSoup官方文档之html树的打印的更多相关文章
- 读BeautifulSoup官方文档之html树的修改
修改html树无非是对其中标签的改动, 改动标签的名字(也就是类型), 属性和标签里的内容... 先讲这边提供了很方便的方法来对其进行改动... soup = BeautifulSoup('<b ...
- 读BeautifulSoup官方文档之html树的搜索(1)
之前介绍了有关的四个对象以及他们的属性, 但是一般情况下要在杂乱的html中提取我们所需的tag(tag中包含的信息)是比较复杂的, 现在我们可以来看看到底有些什么搜索的方法. 最主要的两个方法当然是 ...
- 读BeautifulSoup官方文档之html树的搜索(2)
除了find()和find_all(), 这里还提供了许多类似的方法我就细讲了, 参数和用法都差不多, 最后四个是next, previous是以.next/previous_element()来说的 ...
- 读BeautifulSoup官方文档之与bs有关的对象和属性(1)
自从10号又是5天没更, 是, 我再一次断更... 原因是朋友在搞python, 老问我问题, 我python也是很久没碰了, 于是为了解决他的问题, 我只能重新开始研究python, 为了快速找回感 ...
- 读BeautifulSoup官方文档之与bs有关的对象和属性(2)
上一节说到tag, 这里接着讲, tag有个属性叫做string, tag.string其实就是我们要掌握的四个对象中的第二个 ---- NavigableString, 它代表的是该tag内的te ...
- 读BeautifulSoup官方文档之与bs有关的对象和属性(3)
上一节说到.string的条件很苛刻, 如果某个tag里面包含了超过一个children, 就会返回None, 但是这里提供另外一种方式 .strings, 它返回的是一个generator, 比如对 ...
- 读vue-cli3 官方文档的一些学习记录
原来一直以为vue@cli3 就是创建模板的工具,读了官方文档才知道原来这么有用,不少配置让我长见识了 Prefetch 懒加载配置 懒加载相信大家都是知道的,使用Import() 语法就可以在需要的 ...
- Beautifulsoup官方文档
Beautiful Soup 中文文档 原文 by Leonard Richardson (leonardr@segfault.org) 翻译 by Richie Yan (richieyan@gma ...
- 读jQuery官方文档:$(document).ready()与避免冲突
$(document).ready() 通常你想在DOM结构加载完毕之后才执行相关脚本.使用原生JavaScript,你可能调用window.onload = function() { ... }, ...
随机推荐
- Session or Cookie?是否需要用Tomcat等Web容器的Session
Cookie是HTTP协议标准下的存储用户信息的工具,浏览器把用户信息存放到本地的文本文件中. Session是基于Cookie实现的. 2011年4月,武汉群硕面试的时候(实习生),面试官也问过这个 ...
- [Compose] 8. A curated collection of Monoids and their uses
const { List } = require('immutable-ext'); const Right = x => ({ chain : f => f(x), ap : other ...
- sdo_geometry 转 st_geometry
CREATE OR REPLACE FUNCTION sdo2sde(geo SDO_GEOMETRY) RETURN st_geometry IS lx number; --类型 (点.线.面) c ...
- Navigation Pane不能设置显示标题
https://msdn.microsoft.com/VBA/Word-VBA/articles/view-showheading-method-word https://social.msdn.mi ...
- PatentTips - Well bias control circuit
BACKGROUND OF THE INVENTION The present invention relates to a semiconductor integrated circuit devi ...
- [React Router v4] Style a Link that is Active with NavLink
We often need to be able to apply style to navigation links based on the current route. In React Rou ...
- 【codeforces 546B】Soldier and Badges
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【BZOJ 1005】[HNOI2008]明明的烦恼(化简的另一种方法)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1005 [题意] [题解] 题目和题解在上一篇; 这里 对 [(m^(n-2-tot)) ...
- 课后作业 04 --DateTime应用,判断多久后生日之类
try { Console.Write("请以年-月-日的形式输入您的生日:"); string strA = Console.ReadLine(); DateTime bir = ...
- tombstone问题分析
tombstone文件包含了发生问题的进程ID信息 I/DEBUG ( 241): pid: 244, tid: 244, name: mediaserver >>> /system ...