id: 0546927d544348a48c7e3212be18cb31
parent_id: 
item_type: 1
item_id: 2722157757bd406881024ee58c300128
item_updated_time: 1764345576787
title_diff: "[{\"diffs\":[[1,\"Beautiful Soup 4.2.0 文档 — Beautiful Soup 4.2.\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":45}]"
body_diff: "[{\"diffs\":[[1,\"<html>\\\r\\\n<head>\\\r\\\n  <title>Beautiful Soup 4.2.0 文档 — Beautiful Soup 4.2.0 documentation</title>\\\r\\\n  <basefont face=\\\"微软雅黑\\\" size=\\\"2\\\" />\\\r\\\n  <meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html;charset=utf-8\\\" />\\\r\\\n  <meta name=\\\"exporter-version\\\" content=\\\"YXBJ Windows/608924 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;\\\"/>\\\r\\\n  <meta name=\\\"application-data:2525275\\\" content=\\\"6692893/s30/0cc97f5c-f1a3-419d-914c-b08567b4707f/app.yinxiang.com\\\"/>\\\r\\\n  <style>\\\r\\\n\\tbody, td {\\\r\\\n\\t  font-family: 微软雅黑;\\\r\\\n\\t  font-size: 12pt;\\\r\\\n\\t}\\\r\\\n  </style>\\\r\\\n</head>\\\r\\\n<body>\\\r\\\n<a name=\\\"13309\\\"></a>\\\r\\\n<h1>Beautiful Soup 4.2.0 文档 — Beautiful Soup 4.2.0 documentation</h1>\\\r\\\n\\\r\\\n<div>\\\n<span style=\\\"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\\\"><br/><div style=\\\"font-size: 16px\\\"><div style=\\\"font-family: sans-serif; font-size: 100%; background-color: rgb(17, 48, 61);\\\"><div style=\\\"background-color: rgb(28, 78, 99);\\\"><div style=\\\"background-color: rgb(255, 255, 255);\\\"><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">Beautiful Soup 4.2.0 文档<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#beautiful-soup-4-2-0\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><img alt=\\\"_static/cover.jpg\\\" height=\\\"140\\\" style=\\\"border:0px;text-align:right;clear:right;float:right;margin-left:1em;\\\" width=\\\"140\\\"></img><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Beautiful Soup</a> 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.</p><p style=\\\"text-align:justify;line-height:130%;\\\">这篇文档介绍了BeautifulSoup4中所有主要特性,并且有小例子.让我来向你展示它适合做什么,如何工作,怎样使用,如何达到你想要的效果,和处理异常情况.</p><p style=\\\"text-align:justify;line-height:130%;\\\">文档中出现的例子在Python2.7和Python3.2中的执行结果相同</p><p style=\\\"text-align:justify;line-height:130%;\\\">你可能在寻找 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Beautiful Soup3</a> 的文档,Beautiful Soup 3 目前已经停止开发,我们推荐在现在的项目中使用Beautiful Soup 4, <a href=\\\"http://www.baidu.com/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">移植到BS4</a></p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">寻求帮助<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id1\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">如果你有关于BeautifulSoup的问题,可以发送邮件到 <a href=\\\"https://groups.google.com/forum/?fromgroups#!forum/beautifulsoup\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">讨论组</a> .如果你的问题包含了一段需要转换的HTML代码,那么确保你提的问题描述中附带这段HTML文档的 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id60\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">代码诊断</a> <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id82\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[1]</a></p></div></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">快速开始<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id4\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">下面的一段HTML代码将作为例子被多次用到.这是 <em>爱丽丝梦游仙境的</em> 的一段内容(以后内容中简称为 <em>爱丽丝</em> 的文档):</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>html_doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;body&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt; and</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;;</span><span style=\\\"color:rgb(64, 112, 160);\\\">and they lived at the bottom of a well.&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">使用BeautifulSoup解析这段代码,能够得到一个 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 的对象,并能按照标准的缩进格式的结构输出:</p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><font color=\\\"#FF0000\\\"><span style=\\\"font-weight: bold;\\\">from</span> <span style=\\\"font-weight: bold;\\\">bs4</span> <span style=\\\"font-weight: bold;\\\">import</span> BeautifulSoup</font><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(102, 102, 102);\\\">=</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(51, 51, 51);\\\">BeautifulSoup</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(51, 51, 51);\\\">html_doc</span><span style=\\\"color: rgb(51, 51, 51);\\\">)</span><font color=\\\"#333333\\\">\\\n\\\n</font><span style=\\\"color: rgb(0, 112, 32); font-weight: bold;\\\">print</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">prettify</span><span style=\\\"color: rgb(51, 51, 51);\\\">())</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># &lt;html&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;head&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;title&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    The Dormouse's story</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;/title&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;/head&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;body&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;p class=&quot;title&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;b&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#     The Dormouse's story</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;/b&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;/p&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;p class=&quot;story&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    Once upon a time there were three little sisters; and their names were</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#     Elsie</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;/a&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    ,</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#     Lacie</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;/a&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    and</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link2&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#     Tillie</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    &lt;/a&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    ; and they lived at the bottom of a well.</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;/p&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;p class=&quot;story&quot;&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#    ...</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#   &lt;/p&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;/body&gt;</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># &lt;/html&gt;</span></pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">几个简单的浏览结构化数据的方法:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'title'</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'The Dormouse's story'</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'head'</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'title'</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;link3&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">从文档中找到所有&lt;a&gt;标签的链接:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>link</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a'</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>get</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'href'</span><span>))</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># http://example.com/elsie</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># http://example.com/lacie</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># http://example.com/tillie</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">从文档中获取所有文字内容:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>get_text</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># The Dormouse's story</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># The Dormouse's story</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Once upon a time there were three little sisters; and their names were</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Elsie,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Lacie and</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Tillie;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># and they lived at the bottom of a well.</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ...</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">这是你想要的吗?别着急,还有更好用的</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">安装 Beautiful Soup<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id5\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"text-align:justify;line-height:130%;\\\">如果你用的是新版的Debain或ubuntu,那么可以通过系统的软件包管理来安装:</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>apt-get</span> <span>install</span> <span>Python-bs4</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup 4 通过PyPi发布,所以如果你无法使用系统包管理安装,那么也可以通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>easy_install</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>pip</span></tt> 来安装.包的名字是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>beautifulsoup4</span></tt> ,这个包兼容Python2和Python3.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>easy_install</span> <span>beautifulsoup4</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>pip</span> <span>install</span> <span>beautifulsoup4</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">(在PyPi中还有一个名字是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 的包,但那可能不是你想要的,那是 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Beautiful Soup3</a> 的发布版本,因为很多项目还在使用BS3, 所以 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 包依然有效.但是如果你在编写新项目,那么你应该安装的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>beautifulsoup4</span></tt> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果你没有安装 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>easy_install</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>pip</span></tt> ,那你也可以 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/download/4.x/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">下载BS4的源码</a> ,然后通过setup.py来安装.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>Python</span> <span>setup.py</span> <span>install</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">如果上述安装方法都行不通,Beautiful Soup的发布协议允许你将BS4的代码打包在你的项目中,这样无须安装即可使用.</p><p style=\\\"text-align:justify;line-height:130%;\\\">作者在Python2.7和Python3.2的版本下开发Beautiful Soup, 理论上Beautiful Soup应该在所有当前的Python版本中正常工作</p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">安装完成后的问题<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id8\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup发布时打包成Python2版本的代码,在Python3环境下安装时,会自动转换成Python3的代码,如果没有一个安装的过程,那么代码就不会被转换.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果代码抛出了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError</span></tt> 的异常: “No module named HTMLParser”, 这是因为你在Python3版本中执行Python2版本的代码.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果代码抛出了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError</span></tt> 的异常: “No module named html.parser”, 这是因为你在Python2版本中执行Python3版本的代码.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果遇到上述2种情况,最好的解决方法是重新安装BeautifulSoup4.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果在ROOT_TAG_NAME = u’[document]’代码处遇到 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SyntaxError</span></tt> “Invalid syntax”错误,需要将把BS4的Python代码版本从Python2转换到Python3. 可以重新安装BS4:</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>Python3</span> <span>setup.py</span> <span>install</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">或在bs4的目录中执行Python代码版本转换脚本</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>2to3-3.2</span> <span>-w</span> <span>bs4</span></tt></p></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">安装解析器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 <a href=\\\"http://lxml.de/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">lxml</a> .根据操作系统不同,可以选择下列方法来安装lxml:</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>apt-get</span> <span>install</span> <span>Python-lxml</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>easy_install</span> <span>lxml</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>pip</span> <span>install</span> <span>lxml</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">另一个可供选择的解析器是纯Python实现的 <a href=\\\"http://code.google.com/p/html5lib/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">html5lib</a> , html5lib的解析方式与浏览器相同,可以选择下列方法来安装html5lib:</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>apt-get</span> <span>install</span> <span>Python-html5lib</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>easy_install</span> <span>html5lib</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>pip</span> <span>install</span> <span>html5lib</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">下表列出了主要的解析器,以及它们的优缺点:</p><table border=\\\"1\\\" style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col width=\\\"22%\\\"></col><col width=\\\"26%\\\"></col><col width=\\\"26%\\\"></col><col width=\\\"26%\\\"></col></colgroup><thead valign=\\\"bottom\\\"><tr><th style=\\\"text-align:left;background-color:rgb(238, 221, 238);padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">解析器</th><th style=\\\"text-align:left;background-color:rgb(238, 221, 238);padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">使用方法</th><th style=\\\"text-align:left;background-color:rgb(238, 221, 238);padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">优势</th><th style=\\\"text-align:left;background-color:rgb(238, 221, 238);padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">劣势</th></tr></thead><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">Python标准库</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(markup,</span> <span>&quot;html.parser&quot;)</span></tt></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">Python的内置标准库</li><li style=\\\"text-align:justify;line-height:130%;\\\">执行速度适中</li><li style=\\\"text-align:justify;line-height:130%;\\\">文档容错能力强</li></ul></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差</li></ul></td></tr><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">lxml HTML 解析器</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(markup,</span> <span>&quot;lxml&quot;)</span></tt></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">速度快</li><li style=\\\"text-align:justify;line-height:130%;\\\">文档容错能力强</li></ul></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">需要安装C语言库</li></ul></td></tr><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">lxml XML 解析器</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><p style=\\\"margin-top:0px;text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(markup,</span> <span>[&quot;lxml&quot;,</span> <span>&quot;xml&quot;])</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(markup,</span> <span>&quot;xml&quot;)</span></tt></p></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">速度快</li><li style=\\\"text-align:justify;line-height:130%;\\\">唯一支持XML的解析器</li></ul></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">需要安装C语言库</li></ul></td></tr><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\">html5lib</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(markup,</span> <span>&quot;html5lib&quot;)</span></tt></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">最好的容错性</li><li style=\\\"text-align:justify;line-height:130%;\\\">以浏览器的方式解析文档</li><li style=\\\"text-align:justify;line-height:130%;\\\">生成HTML5格式的文档</li></ul></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border-width:0px 0px 1px;border-bottom-style:solid;border-bottom-color:rgb(170, 170, 170);\\\"><ul style=\\\"margin-top:0px;\\\"><li style=\\\"text-align:justify;line-height:130%;\\\">速度慢</li><li style=\\\"text-align:justify;line-height:130%;\\\">不依赖外部扩展</li></ul></td></tr></tbody></table><p style=\\\"text-align:justify;line-height:130%;\\\">推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.</p><p style=\\\"text-align:justify;line-height:130%;\\\">提示: 如果一段HTML或XML文档格式不正确的话,那么在不同的解析器中返回的结果可能是不一样的,查看 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id49\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">解析器之间的区别</a> 了解更多细节</p></div></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">如何使用<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id10\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">将一段文档传入BeautifulSoup 的构造方法,就能得到一个文档的对象, 可以传入一段字符串或一个文件句柄.</p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><span style=\\\"color: rgb(0, 112, 32); font-weight: bold;\\\">from</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(14, 132, 181); font-weight: bold;\\\">bs4</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(0, 112, 32); font-weight: bold;\\\">import</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(51, 51, 51);\\\">BeautifulSoup</span><font color=\\\"#333333\\\">\\\n\\\n</font><font color=\\\"#EB0073\\\">soup = BeautifulSoup(open(&quot;index.html&quot;))</font><font color=\\\"#333333\\\">\\\n\\\n</font><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(102, 102, 102);\\\">=</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(51, 51, 51);\\\">BeautifulSoup</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(64, 112, 160);\\\">&quot;&lt;html&gt;data&lt;/html&gt;&quot;</span><span style=\\\"color: rgb(51, 51, 51);\\\">)</span></pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">首先,文档被转换成Unicode,并且HTML的实例都被转换成Unicode编码</p><div style=\\\"color: rgb(0, 0, 0);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\nBeautifulSoup(&quot;Sacr&amp;eacute; bleu!&quot;)\\\n&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Sacré bleu!&lt;/body&gt;&lt;/html&gt;\\\n</pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">然后,Beautiful Soup选择最合适的解析器来解析这段文档,如果手动指定解析器那么Beautiful Soup会选择指定的解析器来解析文档.(参考 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#xml\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">解析成XML</a> ).</p></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">对象的种类<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id11\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Comment</span></tt> .</p><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\">Tag<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#tag\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> 对象与XML或HTML原生文档中的tag相同:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;b class=&quot;boldest&quot;&gt;Extremely bold&lt;/b&gt;'</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(0, 112, 32);\\\">type</span><span>(</span><span>tag</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;class 'bs4.element.Tag'&gt;</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">Tag有很多方法和属性,在 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id15\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">遍历文档树</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">搜索文档树</a> 中有详细解释.现在介绍一下tag中最重要的属性: name和attributes</p><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">Name<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#name\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">每个tag都有自己的名字,通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.name</span></tt> 来获取:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'b'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果改变了tag的name,那将影响所有通过当前Beautiful Soup对象生成的HTML文档:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;blockquote&quot;</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote class=&quot;boldest&quot;&gt;Extremely bold&lt;/blockquote&gt;</span></pre></div></div><div><h3 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 140%;\\\">Attributes<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#attributes\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">一个tag可能有很多个属性. tag <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>&lt;b</span> <span>class=&quot;boldest&quot;&gt;</span></tt> 有一个 “class” 的属性,值为 “boldest” . tag的属性的操作方法与字典相同:</p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><font color=\\\"#FF0000\\\">tag['class']</font><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># u'boldest'</span></pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">也可以直接”点”取属性, 比如: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.attrs</span></tt> :</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>attrs</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># {u'class': u'boldest'}</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">tag的属性可以被添加,删除或修改. 再说一次, tag的属性操作方法与字典一样</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'verybold'</span><span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'id'</span><span>]</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(32, 128, 80);\\\">1</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote class=&quot;verybold&quot; id=&quot;1&quot;&gt;Extremely bold&lt;/blockquote&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">del</span> <span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">del</span> <span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'id'</span><span>]</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote&gt;Extremely bold&lt;/blockquote&gt;</span>\\\n\\\n<span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># KeyError: 'class'</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>get</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h4 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:120%;\\\">多值属性<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id12\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h4><p style=\\\"text-align:justify;line-height:130%;\\\">HTML 4定义了一系列可以包含多个值的属性.在HTML5中移除了一些,却增加更多.最常见的多值的属性是 class (一个tag可以有多个CSS的class). 还有一些属性 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>rel</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>rev</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>accept-charset</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>headers</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>accesskey</span></tt> . 在Beautiful Soup中多值属性的返回类型是list:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>css_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;'</span><span>)</span><span>css_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&quot;body&quot;, &quot;strikeout&quot;]</span>\\\n\\\n<span>css_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p class=&quot;body&quot;&gt;&lt;/p&gt;'</span><span>)</span><span>css_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&quot;body&quot;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果某个属性看起来好像有多个值,但在任何版本的HTML定义中都没有被定义为多值属性,那么Beautiful Soup会将这个属性作为字符串返回</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>id_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p id=&quot;my id&quot;&gt;&lt;/p&gt;'</span><span>)</span><span>id_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'id'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 'my id'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">将tag转换成字符串时,多值属性会合并为一个值</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>rel_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p&gt;Back to the &lt;a rel=&quot;index&quot;&gt;homepage&lt;/a&gt;&lt;/p&gt;'</span><span>)</span><span>rel_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'rel'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ['index']</span><span>rel_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'rel'</span><span>]</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'index'</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'contents'</span><span>]</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>rel_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p&gt;Back to the &lt;a rel=&quot;index contents&quot;&gt;homepage&lt;/a&gt;&lt;/p&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果转换的文档是XML格式,那么tag中不包含多值属性</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>xml_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;'</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'xml'</span><span>)</span><span>xml_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'body strikeout'</span></pre></div></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">可以遍历的字符串<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id13\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">字符串常被包含在tag内.Beautiful Soup用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 类来包装tag中的字符串:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Extremely bold'</span><span style=\\\"color:rgb(0, 112, 32);\\\">type</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;class 'bs4.element.NavigableString'&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">一个 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 字符串与Python中的Unicode字符串相同,并且还支持包含在 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id15\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">遍历文档树</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">搜索文档树</a> 中的一些特性. 通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>unicode()</span></tt> 方法可以直接将 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 对象转换成Unicode字符串:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>unicode_string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(0, 112, 32);\\\">unicode</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span>)</span><span>unicode_string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Extremely bold'</span><span style=\\\"color:rgb(0, 112, 32);\\\">type</span><span>(</span><span>unicode_string</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;type 'unicode'&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">tag中包含的字符串不能编辑,但是可以被替换成其它的字符串,用 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#replace-with\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">replace_with()</a> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>replace_with</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;No longer bold&quot;</span><span>)</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote&gt;No longer bold&lt;/blockquote&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 对象支持 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id15\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">遍历文档树</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">搜索文档树</a> 中定义的大部分属性, 并非全部.尤其是,一个字符串不能包含其它内容(tag能够包含字符串或是其它tag),字符串不支持 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contents</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 属性或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果想在Beautiful Soup之外使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 对象,需要调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>unicode()</span></tt> 方法,将该对象转换成普通的Unicode字符串,否则就算Beautiful Soup已方法已经执行结束,该对象的输出也会带有对象的引用地址.这样会浪费内存.</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">BeautifulSoup<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#beautifulsoup\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> 对象,它支持 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id15\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">遍历文档树</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">搜索文档树</a> 中描述的大部分的方法.</p><p style=\\\"text-align:justify;line-height:130%;\\\">因为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象并不是真正的HTML或XML的tag,所以它没有name和attribute属性.但有时查看它的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.name</span></tt> 属性是很方便的,所以 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象包含了一个值为 “[document]” 的特殊属性 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.name</span></tt></p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'[document]'</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">注释及特殊字符串<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id14\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 几乎覆盖了html和xml中的所有内容,但是还有一些特殊对象.容易让人担心的内容是文档的注释部分:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;b&gt;&lt;!--Hey, buddy. Want to buy a used parser?--&gt;&lt;/b&gt;&quot;</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>comment</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(0, 112, 32);\\\">type</span><span>(</span><span>comment</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;class 'bs4.element.Comment'&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Comment</span></tt> 对象是一个特殊类型的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 对象:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>comment</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Hey, buddy. Want to buy a used parser'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">但是当它出现在HTML文档中时, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Comment</span></tt> 对象会使用特殊的格式输出:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;!--Hey, buddy. Want to buy a used parser?--&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/b&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup中定义的其它类型都可能会出现在XML的文档中: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>CData</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ProcessingInstruction</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Declaration</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Doctype</span></tt> .与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Comment</span></tt> 对象类似,这些类都是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 的子类,只是添加了一些额外的方法的字符串独享.下面是用CDATA来替代注释的例子:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>CData</span><span>cdata</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>CData</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;A CDATA block&quot;</span><span>)</span><span>comment</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>replace_with</span><span>(</span><span>cdata</span><span>)</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;![CDATA[A CDATA block]]&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/b&gt;</span></pre></div></div></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">遍历文档树<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id15\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">还拿”爱丽丝梦游仙境”的文档来做例子:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>html_doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt; and</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;;</span><span style=\\\"color:rgb(64, 112, 160);\\\">and they lived at the bottom of a well.&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>BeautifulSoup</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>)</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">通过这段例子来演示怎样从文档的一段内容找到另一段内容</p><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\">子节点<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id16\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">一个Tag可能包含多个字符串或其它的Tag,这些都是这个Tag的子节点.Beautiful Soup提供了许多操作和遍历子节点的属性.</p><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">注意: Beautiful Soup中字符串节点不支持这些属性,因为字符串没有子节点</p><div><h3 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 140%;\\\">tag的名字<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id17\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">操作文档树最简单的方法就是告诉它你想获取的tag的name.如果想获取 &lt;head&gt; 标签,只要用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>soup.head</span></tt> :</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>head</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">这是个获取tag的小窍门,可以在文档树的tag中多次调用这个方法.下面的代码可以获取&lt;body&gt;标签中的第一个&lt;b&gt;标签:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>body</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;The Dormouse's story&lt;/b&gt;</span></pre></div></div><p style=\\\"text-align: justify; line-height: 130%;\\\"><font color=\\\"#FF0000\\\">通过点取属性的方式只能获得当前名字的第一个tag:</font></p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">如果想要得到所有的&lt;a&gt;标签,或是通过名字得到比一个tag更多的内容的时候,就需要用到 <cite>Searching the tree</cite> 中描述的方法,比如: find_all()</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.contents 和 .children<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#contents-children\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contents</span></tt> 属性可以将tag的子节点以列表的方式输出:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\nhead_tag = soup.head\\\nhead_tag\\\n# &lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;\\\n\\\nhead_tag.contents\\\n[&lt;title&gt;The Dormouse's story&lt;/title&gt;]\\\n\\\ntitle_tag = head_tag.contents[0]\\\ntitle_tag\\\n# &lt;title&gt;The Dormouse's story&lt;/title&gt;\\\ntitle_tag.contents\\\n# [u'The Dormouse's story']\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象本身一定会包含子节点,也就是说&lt;html&gt;标签也是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的子节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);\\\">len</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 1</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span>[</span><span style=\\\"color:rgb(32, 128, 80);\\\">0</span><span>]</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'html'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">字符串没有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contents</span></tt> 属性,因为字符串没有子节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>text</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>title_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span>[</span><span style=\\\"color:rgb(32, 128, 80);\\\">0</span><span>]</span><span>text</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># AttributeError: 'NavigableString' object has no attribute 'contents'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.children</span></tt> 生成器,可以对tag的子节点进行循环:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>child</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>title_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>children</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>child</span><span>)</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># The Dormouse's story</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.descendants<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#descendants\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contents</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.children</span></tt> 属性仅包含tag的直接子节点.例如,&lt;head&gt;标签只有一个直接子节点&lt;title&gt;</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>head_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">但是&lt;title&gt;标签也包含一个子节点:字符串 “The Dormouse’s story”,这种情况下字符串 “The Dormouse’s story”也属于&lt;head&gt;标签的子孙节点. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.descendants</span></tt> 属性可以对所有tag的子孙节点进行递归循环 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id86\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[5]</a> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>child</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>head_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>descendants</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>child</span><span>)</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># The Dormouse's story</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">上面的例子中, &lt;head&gt;标签只有一个子节点,但是有2个子孙节点:&lt;head&gt;节点和&lt;head&gt;的子节点, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 有一个直接子节点(&lt;html&gt;节点),却有很多子孙节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);\\\">len</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">list</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>children</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 1</span><span style=\\\"color:rgb(0, 112, 32);\\\">len</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">list</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>descendants</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 25</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.string<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#string\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">如果tag只有一个 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 类型子节点,那么这个tag可以使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 得到子节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>title_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'The Dormouse's story'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果一个tag仅有一个子节点,那么这个tag也可以使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 方法,输出结果与当前唯一子节点的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 结果相同:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>head_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span>\\\n\\\n<span>head_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'The Dormouse's story'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果tag包含了多个子节点,tag就无法确定 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 方法应该调用哪个子节点的内容, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 的输出结果是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>html</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.strings 和 stripped_strings<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#strings-stripped-strings\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">如果tag中包含多个字符串 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id83\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[2]</a> ,可以使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.strings</span></tt> 来循环获取:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>string</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>strings</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">repr</span><span>(</span><span>string</span><span>))</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u&quot;The Dormouse's story&quot;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u&quot;The Dormouse's story&quot;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Once upon a time there were three little sisters; and their names were\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Elsie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u',\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Lacie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u' and\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Tillie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u';\\\\\nand they lived at the bottom of a well.'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'...'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">输出的字符串中可能包含了很多空格或空行,使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.stripped_strings</span></tt> 可以去除多余空白内容:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>string</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>stripped_strings</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">repr</span><span>(</span><span>string</span><span>))</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u&quot;The Dormouse's story&quot;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u&quot;The Dormouse's story&quot;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Once upon a time there were three little sisters; and their names were'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Elsie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u','</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Lacie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'and'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Tillie'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u';\\\\\nand they lived at the bottom of a well.'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'...'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">全部是空格的行会被忽略掉,段首和段末的空白会被删除</p></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">父节点<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id20\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">继续分析文档树,每个tag或字符串都有父节点:被包含在某个tag中</p><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.parent<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#parent\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.parent</span></tt> 属性来获取某个元素的父节点.在例子“爱丽丝”的文档中,&lt;head&gt;标签是&lt;title&gt;标签的父节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>title_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span>title_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span><span>title_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">文档title的字符串也有父节点:&lt;title&gt;标签</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>title_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">文档的顶层节点比如&lt;html&gt;的父节点是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>html_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>html</span><span style=\\\"color:rgb(0, 112, 32);\\\">type</span><span>(</span><span>html_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;class 'bs4.BeautifulSoup'&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.parent</span></tt> 是None:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.parents<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#parents\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">通过元素的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.parents</span></tt> 属性可以递归得到元素的所有父辈节点,下面的例子使用了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.parents</span></tt> 方法遍历了&lt;a&gt;标签到根节点的所有节点.</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>parent</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parents</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">if</span> <span>parent</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">is</span> <span style=\\\"color:rgb(0, 112, 32);\\\">None</span><span>:</span>\\\n        <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>parent</span><span>)</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">else</span><span>:</span>\\\n        <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>parent</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># body</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># html</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [document]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">兄弟节点<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id21\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">看一段简单的例子:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>sibling_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;b&gt;text1&lt;/b&gt;&lt;c&gt;text2&lt;/c&gt;&lt;/b&gt;&lt;/a&gt;&quot;</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;a&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;b&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#     text1</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;/b&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;c&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#     text2</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;/c&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;/a&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">因为&lt;b&gt;标签和&lt;c&gt;标签是同一层:他们是同一个元素的子节点,所以&lt;b&gt;和&lt;c&gt;可以被称为兄弟节点.一段文档以标准格式输出时,兄弟节点有相同的缩进级别.在代码中也可以使用这种关系.</p><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.next_sibling 和 .previous_sibling<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-sibling-previous-sibling\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">在文档树中,使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_sibling</span></tt> 属性来查询兄弟节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;c&gt;text2&lt;/c&gt;</span>\\\n\\\n<span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>c</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_sibling</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;text1&lt;/b&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">&lt;b&gt;标签有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 属性,但是没有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_sibling</span></tt> 属性,因为&lt;b&gt;标签在同级节点中是第一个.同理,&lt;c&gt;标签有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_sibling</span></tt> 属性,却没有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_sibling</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>c</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">例子中的字符串“text1”和“text2”不是兄弟节点,因为它们的父节点不同:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'text1'</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>sibling_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">实际文档中的tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_sibling</span></tt> 属性通常是字符串或空白. 看看“爱丽丝”文档:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\n&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;\\\n&lt;a href=&quot;http://example.com/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;\\\n&lt;a href=&quot;http://example.com/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果以为第一个&lt;a&gt;标签的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 结果是第二个&lt;a&gt;标签,那就错了,真实结果是第一个&lt;a&gt;标签和第二个&lt;a&gt;标签之间的顿号和换行符:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n\\\n<span>link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u',\\\\\n'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">第二个&lt;a&gt;标签是顿号的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;</span></pre></div></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.next_siblings 和 .previous_siblings<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-siblings-previous-siblings\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_siblings</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_siblings</span></tt> 属性可以对当前节点的兄弟节点迭代输出:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>sibling</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_siblings</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">repr</span><span>(</span><span>sibling</span><span>))</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u',\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u' and\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'; and they lived at the bottom of a well.'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>sibling</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;link3&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_siblings</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">repr</span><span>(</span><span>sibling</span><span>))</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ' and\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u',\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Once upon a time there were three little sisters; and their names were\\\\\n'</span>\\\n    <span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">回退和前进<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id22\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">看一下“爱丽丝” 文档:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\n&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;\\\n&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">HTML解析器把这段字符串转换成一连串的事件: “打开&lt;html&gt;标签”,”打开一个&lt;head&gt;标签”,”打开一个&lt;title&gt;标签”,”添加一段字符串”,”关闭&lt;title&gt;标签”,”打开&lt;p&gt;标签”,等等.Beautiful Soup提供了重现解析器初始化过程的方法.</p><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.next_element 和 .previous_element<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-element-previous-element\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_element</span></tt> 属性指向解析过程中下一个被解析的对象(字符串或tag),结果可能与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 相同,但通常是不一样的.</p><p style=\\\"text-align:justify;line-height:130%;\\\">这是“爱丽丝”文档中最后一个&lt;a&gt;标签,它的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_sibling</span></tt> 结果是一个字符串,因为当前的解析过程 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id83\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[2]</a> 因为当前的解析过程因为遇到了&lt;a&gt;标签而中断了:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>last_a_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;link3&quot;</span><span>)</span><span>last_a_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;</span>\\\n\\\n<span>last_a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_sibling</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '; and they lived at the bottom of a well.'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">但这个&lt;a&gt;标签的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_element</span></tt> 属性结果是在&lt;a&gt;标签被解析之后的解析内容,不是&lt;a&gt;标签后的句子部分,应该是字符串”Tillie”:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>last_a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_element</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Tillie'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">这是因为在原始文档中,字符串“Tillie” 在分号前出现,解析器先进入&lt;a&gt;标签,然后是字符串“Tillie”,然后关闭&lt;/a&gt;标签,然后是分号和剩余部分.分号与&lt;a&gt;标签在同一层级,但是字符串“Tillie”会被先解析.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_element</span></tt> 属性刚好与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_element</span></tt> 相反,它指向当前被解析的对象的前一个解析对象:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>last_a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_element</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u' and\\\\\n'</span><span>last_a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_element</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_element</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;</span></pre></div></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">.next_elements 和 .previous_elements<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-elements-previous-elements\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.next_elements</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.previous_elements</span></tt> 的迭代器就可以向前或向后访问文档的解析内容,就好像文档正在被解析一样:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>element</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>last_a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_elements</span><span>:</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">repr</span><span>(</span><span>element</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Tillie'</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u';\\\\\nand they lived at the bottom of a well.'</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n\\\\\n'</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'...'</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'\\\\\n'</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div></div></div></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">搜索文档树<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">Beautiful Soup定义了很多搜索方法,这里着重介绍2个: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> .其它方法的参数和用法类似,请读者举一反三.</p><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">再以“爱丽丝”文档作为例子:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>html_doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt; and</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;;</span><span style=\\\"color:rgb(64, 112, 160);\\\">and they lived at the bottom of a well.&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>BeautifulSoup</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>)</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 类似的方法可以查找到想要查找的文档内容</p><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\">过滤器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id25\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">介绍 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法前,先介绍一下过滤器的类型 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id84\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[3]</a> ,这些过滤器贯穿整个搜索的API.过滤器可以被用在tag的name中,节点的属性中,字符串中或他们的混合中.</p><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">字符串<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id27\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的&lt;b&gt;标签:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'b'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;b&gt;The Dormouse's story&lt;/b&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果传入字节码参数,Beautiful Soup会当作UTF-8编码,可以传入一段Unicode 编码来避免Beautiful Soup解析编码出错</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">正则表达式<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id28\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>match()</span></tt> 来匹配内容.下面例子中找出所有以b开头的标签,这表示&lt;body&gt;和&lt;b&gt;标签都应该被找到:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">re</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>tag</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>re</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>compile</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;^b&quot;</span><span>)):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># body</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># b</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">下面代码找出所有名字中包含”t”的标签:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>tag</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>re</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>compile</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;t&quot;</span><span>)):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># html</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># title</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">列表<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id29\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有&lt;a&gt;标签和&lt;b&gt;标签:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>([</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;b&quot;</span><span>])</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;b&gt;The Dormouse's story&lt;/b&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">True<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#true\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt> 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>tag</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">True</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># html</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># head</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># title</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># body</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># b</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span></pre></div></div><div><h3 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 140%;\\\">方法<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id30\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id85\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[4]</a> ,如果这个方法返回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt> 表示当前元素匹配并且被找到,如果不是则反回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>False</span></tt></p><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">下面方法校验了当前元素,如果包含 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 属性却不包含 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>id</span></tt> 属性,那么将返回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt>:</p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><font color=\\\"#FF0000\\\"><span style=\\\"font-weight: bold;\\\">def</span> has_class_but_no_id(tag):\\\n    <span style=\\\"font-weight: bold;\\\">return</span> tag.has_attr('class') <span style=\\\"font-weight: bold;\\\">and</span> <span style=\\\"font-weight: bold;\\\">not</span> tag.has_attr('id')</font></pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">将这个方法作为参数传入 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法,将得到所有&lt;p&gt;标签:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>has_class_but_no_id</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;p class=&quot;story&quot;&gt;Once upon a time there were...&lt;/p&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;]</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">返回结果中只有&lt;p&gt;标签没有&lt;a&gt;标签,因为&lt;a&gt;标签还定义了”id”,没有返回&lt;html&gt;和&lt;head&gt;,因为&lt;html&gt;和&lt;head&gt;中没有定义”class”属性.</p><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">下面代码找到所有被文字包含的节点内容:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>NavigableString</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">def</span> <span style=\\\"color:rgb(6, 40, 126);\\\">surrounded_by_strings</span><span>(</span><span>tag</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">return</span> <span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">isinstance</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>next_element</span><span>,</span> <span>NavigableString</span><span>)</span>\\\n            <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">and</span> <span style=\\\"color:rgb(0, 112, 32);\\\">isinstance</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>previous_element</span><span>,</span> <span>NavigableString</span><span>))</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>tag</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>surrounded_by_strings</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span> <span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># a</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># p</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">现在来了解一下搜索方法的细节</p></div></div><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\">find_all()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">find_all( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件.这里有几个例子:</p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">find_all</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(64, 112, 160);\\\">&quot;title&quot;</span><span style=\\\"color: rgb(51, 51, 51);\\\">)</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span><font color=\\\"#333333\\\">\\\n\\\n</font><font color=\\\"#FF0000\\\">soup.find_all(&quot;p&quot;, &quot;title&quot;)\\\n<span style=\\\"font-style: italic;\\\"># [&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;]</span></font><font color=\\\"#333333\\\">\\\n\\\n</font><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">find_all</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(64, 112, 160);\\\">&quot;a&quot;</span><span style=\\\"color: rgb(51, 51, 51);\\\">)</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span><font color=\\\"#333333\\\">\\\n\\\n</font><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">find_all</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(0, 112, 32);\\\">id</span><span style=\\\"color: rgb(102, 102, 102);\\\">=</span><span style=\\\"color: rgb(64, 112, 160);\\\">&quot;link2&quot;</span><span style=\\\"color: rgb(51, 51, 51);\\\">)</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span><font color=\\\"#333333\\\">\\\n\\\n</font><span style=\\\"color: rgb(0, 112, 32); font-weight: bold;\\\">import</span><font color=\\\"#333333\\\"> </font><span style=\\\"color: rgb(14, 132, 181); font-weight: bold;\\\">re</span><span style=\\\"color: rgb(51, 51, 51);\\\">soup</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">find</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(51, 51, 51);\\\">text</span><span style=\\\"color: rgb(102, 102, 102);\\\">=</span><span style=\\\"color: rgb(51, 51, 51);\\\">re</span><span style=\\\"color: rgb(102, 102, 102);\\\">.</span><span style=\\\"color: rgb(51, 51, 51);\\\">compile</span><span style=\\\"color: rgb(51, 51, 51);\\\">(</span><span style=\\\"color: rgb(64, 112, 160);\\\">&quot;sisters&quot;</span><span style=\\\"color: rgb(51, 51, 51);\\\">))</span><span style=\\\"color: rgb(64, 128, 144); font-style: italic;\\\"># u'Once upon a time there were three little sisters; and their names were\\\\\n'</span></pre></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">有几个方法很相似,还有几个方法是新的,参数中的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>text</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>id</span></tt> 是什么含义? 为什么 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all(&quot;p&quot;,</span> <span>&quot;title&quot;)</span></tt> 返回的是CSS Class为”title”的&lt;p&gt;标签? 我们来仔细看一下 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 的参数</p><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">name 参数<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>name</span></tt> 参数可以查找所有名字为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>name</span></tt> 的tag,字符串对象会被自动忽略掉.</p><p style=\\\"text-align:justify;line-height:130%;\\\">简单的用法如下:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">重申: 搜索 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>name</span></tt> 参数的值可以使任一类型的 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id25\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">过滤器</a> ,字符窜,正则表达式,列表,方法或是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt> .</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">keyword 参数<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>id</span></tt> 的参数,Beautiful Soup会搜索每个tag的”id”属性.</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">'link2'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果传入 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>href</span></tt> 参数,Beautiful Soup会搜索每个tag的”href”属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>href</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>re</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>compile</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;elsie&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">搜索指定名字的属性时可以使用的参数值包括 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id27\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">字符串</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id28\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">正则表达式</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id29\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">列表</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#true\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">True</a> .</p><p style=\\\"text-align:justify;line-height:130%;\\\">下面的例子在文档树中查找所有包含 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>id</span></tt> 属性的tag,无论 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>id</span></tt> 的值是什么:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(0, 112, 32);\\\">True</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">使用多个指定名字的参数可以同时过滤tag的多个属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>href</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>re</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>compile</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;elsie&quot;</span><span>),</span> <span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">'link1'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;three&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>data_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;div data-foo=&quot;value&quot;&gt;foo!&lt;/div&gt;'</span><span>)</span><span>data_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>data</span><span style=\\\"color:rgb(102, 102, 102);\\\">-</span><span>foo</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;value&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># SyntaxError: keyword can't be an expression</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">但是可以通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>attrs</span></tt> 参数定义一个字典参数来搜索包含特殊属性的tag:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>data_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>attrs</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>{</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;data-foo&quot;</span><span>:</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;value&quot;</span><span>})</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;div data-foo=&quot;value&quot;&gt;foo!&lt;/div&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">按CSS搜索<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">按照CSS类名搜索tag的功能非常实用,但标识CSS类名的关键字 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 在Python中是保留字,使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class_</span></tt> 参数搜索有指定CSS类名的tag:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;sister&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class_</span></tt> 参数同样接受不同类型的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>过滤器</span></tt> ,字符串,正则表达式,方法或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>re</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>compile</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;itl&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;]</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">def</span> <span style=\\\"color:rgb(6, 40, 126);\\\">has_six_characters</span><span>(</span><span>css_class</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">return</span> <span>css_class</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">is</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">not</span> <span style=\\\"color:rgb(0, 112, 32);\\\">None</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">and</span> <span style=\\\"color:rgb(0, 112, 32);\\\">len</span><span>(</span><span>css_class</span><span>)</span> <span style=\\\"color:rgb(102, 102, 102);\\\">==</span> <span style=\\\"color:rgb(32, 128, 80);\\\">6</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>has_six_characters</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 属性是 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id12\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">多值属性</a> .按照CSS类名搜索tag时,可以分别搜索tag中的每个CSS类名:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>css_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;'</span><span>)</span><span>css_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>,</span> <span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;strikeout&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;]</span>\\\n\\\n<span>css_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>,</span> <span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;body&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">搜索 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 属性时也可以通过CSS值完全匹配:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>css_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>,</span> <span>class_</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;body strikeout&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;body strikeout&quot;&gt;&lt;/p&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">完全匹配 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 的值时,如果CSS类名的顺序与实际不符,将搜索不到结果:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span>attrs</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>{</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;class&quot;</span><span>:</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;sister&quot;</span><span>})</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:transparent;\\\"><span>text</span></tt> 参数<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>text</span></tt> 参数可以搜搜文档中的字符串内容.与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>name</span></tt> 参数的可选值一样, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>text</span></tt> 参数接受 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id27\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">字符串</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id28\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">正则表达式</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id29\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">列表</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#true\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">True</a> . 看例子:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\nsoup.find_all(text=&quot;Elsie&quot;)\\\n# [u'Elsie']\\\n\\\nsoup.find_all(text=[&quot;Tillie&quot;, &quot;Elsie&quot;, &quot;Lacie&quot;])\\\n# [u'Elsie', u'Lacie', u'Tillie']\\\n\\\nsoup.find_all(text=re.compile(&quot;Dormouse&quot;))\\\n[u&quot;The Dormouse's story&quot;, u&quot;The Dormouse's story&quot;]\\\n\\\ndef is_the_only_string_within_a_tag(s):\\\n    &quot;&quot;Return True if this string is the only child of its parent tag.&quot;&quot;\\\n    return (s == s.parent.string)\\\n\\\nsoup.find_all(text=is_the_only_string_within_a_tag)\\\n# [u&quot;The Dormouse's story&quot;, u&quot;The Dormouse's story&quot;, u'Elsie', u'Lacie', u'Tillie', u'...']\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">虽然 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>text</span></tt> 参数用于搜索字符串,还可以与其它参数混合使用来过滤tag.Beautiful Soup会找到 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 方法与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>text</span></tt> 参数值相符的tag.下面代码用来搜索内容里面包含“Elsie”的&lt;a&gt;标签:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span>text</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Elsie&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:transparent;\\\"><span>limit</span></tt> 参数<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#limit\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>limit</span></tt> 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>limit</span></tt> 的限制时,就停止搜索返回结果.</p><p style=\\\"text-align:justify;line-height:130%;\\\">文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span>limit</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(32, 128, 80);\\\">2</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:transparent;\\\"><span>recursive</span></tt> 参数<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">调用tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>recursive=False</span></tt> .</p><p style=\\\"text-align:justify;line-height:130%;\\\">一段简单的文档:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\n&lt;html&gt;\\\n &lt;head&gt;\\\n  &lt;title&gt;\\\n   The Dormouse's story\\\n  &lt;/title&gt;\\\n &lt;/head&gt;\\\n...\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">是否使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>recursive</span></tt> 参数的搜索结果:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>html</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>html</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>,</span> <span>recursive</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(0, 112, 32);\\\">False</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># []</span></pre></div></div></div><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\">像调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:transparent;\\\"><span>find_all()</span></tt> 一样调用tag<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all-tag\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align: justify; line-height: 130%;\\\"><font color=\\\"#FF0000\\\"><tt style=\\\"padding: 0px 1px; font-size: 0.95em; background-color: rgb(236, 240, 243);\\\">find_all()</tt> 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. <tt style=\\\"padding: 0px 1px; font-size: 0.95em; background-color: rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象和 <tt style=\\\"padding: 0px 1px; font-size: 0.95em; background-color: rgb(236, 240, 243);\\\"><span>tag</span></tt> 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 <tt style=\\\"padding: 0px 1px; font-size: 0.95em; background-color: rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法相同,下面两行代码是等价的:</font></p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><font color=\\\"#FF0000\\\"><span>soup</span>.<span>find_all</span><span>(</span>&quot;a&quot;<span>)</span><span>soup</span><span>(</span>&quot;a&quot;<span>)</span></font></pre></div><p style=\\\"text-align: justify; line-height: 130%;\\\"><font color=\\\"#FF0000\\\">这两行代码也是等价的:</font></p><div style=\\\"background-color: rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x: auto; overflow-y: hidden; padding: 5px; line-height: 120%; border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: rgb(170, 204, 153); border-bottom-color: rgb(170, 204, 153); background-color: rgb(238, 255, 204);\\\"><font color=\\\"#FF0000\\\">soup.title.find_all(text=True)\\\nsoup.title(text=True)</font></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个&lt;body&gt;标签,那么使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法来查找&lt;body&gt;标签就不太合适, 使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all</span></tt> 方法并设置 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>limit=1</span></tt> 参数不如直接使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法.下面两行代码是等价的:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'title'</span><span>,</span> <span>limit</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(32, 128, 80);\\\">1</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'title'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">唯一的区别是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法的返回结果是值包含一个元素的列表,而 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法直接返回结果.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法没有找到目标是返回空列表, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法找不到目标时,返回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> .</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;nosuchtag&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>soup.head.title</span></tt> 是 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id17\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">tag的名字</a> 方法的简写.这个简写的原理就是多次调用当前tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>head</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>title</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;head&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find_parents() 和 find_parent()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-parents-find-parent\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find_parents( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">find_parent( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">我们已经用了很大篇幅来介绍 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法,Beautiful Soup中还有10个用于搜索的API.它们中的五个用的是与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 相同的搜索参数,另外5个与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法的搜索参数类似.区别仅是它们搜索文档的不同部分.</p><p style=\\\"text-align:justify;line-height:130%;\\\">记住: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 只搜索当前节点的所有子节点,孙子节点等. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parents()</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parent()</span></tt> 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容. 我们从一个文档中的一个叶子节点开始:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\na_string = soup.find(text=&quot;Lacie&quot;)\\\na_string\\\n# u'Lacie'\\\n\\\na_string.find_parents(&quot;a&quot;)\\\n# [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]\\\n\\\na_string.find_parent(&quot;p&quot;)\\\n# &lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were\\\n#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,\\\n#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt; and\\\n#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;;\\\n#  and they lived at the bottom of a well.&lt;/p&gt;\\\n\\\na_string.find_parents(&quot;p&quot;, class=&quot;title&quot;)\\\n# []\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">文档中的一个&lt;a&gt;标签是是当前叶子节点的直接父节点,所以可以被找到.还有一个&lt;p&gt;标签,是目标叶子节点的间接父辈节点,所以也可以被找到.包含class值为”title”的&lt;p&gt;标签不是不是目标叶子节点的父辈节点,所以通过 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parents()</span></tt> 方法搜索不到.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parent()</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parents()</span></tt> 方法会让人联想到 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#parent\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.parent</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#parents\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.parents</a> 属性.它们之间的联系非常紧密.搜索父辈节点的方法实际上就是对 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.parents</span></tt> 属性的迭代搜索.</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find_next_siblings() 合 find_next_sibling()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-next-siblings-find-next-sibling\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find_next_siblings( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">find_next_sibling( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">这2个方法通过 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-siblings-previous-siblings\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.next_siblings</a> 属性对当tag的所有后面解析 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id86\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[5]</a> 的兄弟tag节点进行迭代, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next_siblings()</span></tt> 方法返回所有符合条件的后面的兄弟节点, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next_sibling()</span></tt> 只返回符合条件的后面的第一个tag节点.</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>first_link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>first_link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n\\\n<span>first_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_next_siblings</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>first_story_paragraph</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;story&quot;</span><span>)</span><span>first_story_paragraph</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_next_sibling</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find_previous_siblings() 和 find_previous_sibling()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-previous-siblings-find-previous-sibling\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find_previous_siblings( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">find_previous_sibling( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">这2个方法通过 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-siblings-previous-siblings\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.previous_siblings</a> 属性对当前tag的前面解析 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id86\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[5]</a> 的兄弟tag节点进行迭代, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous_siblings()</span></tt> 方法返回所有符合条件的前面的兄弟节点, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous_sibling()</span></tt> 方法返回第一个符合条件的前面的兄弟节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>last_link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;link3&quot;</span><span>)</span><span>last_link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;</span>\\\n\\\n<span>last_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_previous_siblings</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span>\\\n\\\n<span>first_story_paragraph</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;story&quot;</span><span>)</span><span>first_story_paragraph</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_previous_sibling</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find_all_next() 和 find_next()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all-next-find-next\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find_all_next( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">find_next( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">这2个方法通过 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-elements-previous-elements\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.next_elements</a> 属性对当前tag的之后的 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id86\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[5]</a> tag和字符串进行迭代, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all_next()</span></tt> 方法返回所有符合条件的节点, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next()</span></tt> 方法返回第一个符合条件的节点:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>first_link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>first_link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n\\\n<span>first_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all_next</span><span>(</span><span>text</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(0, 112, 32);\\\">True</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'Elsie', u',\\\\\n', u'Lacie', u' and\\\\\n', u'Tillie',</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  u';\\\\\nand they lived at the bottom of a well.', u'\\\\\n\\\\\n', u'...', u'\\\\\n']</span>\\\n\\\n<span>first_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_next</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">第一个例子中,字符串 “Elsie”也被显示出来,尽管它被包含在我们开始查找的&lt;a&gt;标签的里面.第二个例子中,最后一个&lt;p&gt;标签也被显示出来,尽管它与我们开始查找位置的&lt;a&gt;标签不属于同一部分.例子中,搜索的重点是要匹配过滤器的条件,并且在文档中出现的顺序而不是开始查找的元素的位置.</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">find_all_previous() 和 find_previous()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-all-previous-find-previous\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">find_all_previous( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">find_previous( <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> )</p><p style=\\\"text-align:justify;line-height:130%;\\\">这2个方法通过 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#next-elements-previous-elements\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.previous_elements</a> 属性对当前节点前面 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id86\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[5]</a> 的tag和字符串进行迭代, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all_previous()</span></tt> 方法返回所有符合条件的节点, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous()</span></tt> 方法返回第一个符合条件的节点.</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>first_link</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>first_link</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;</span>\\\n\\\n<span>first_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all_previous</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; ...&lt;/p&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;]</span>\\\n\\\n<span>first_link</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_previous</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;title&gt;The Dormouse's story&lt;/title&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all_previous(&quot;p&quot;)</span></tt> 返回了文档中的第一段(class=”title”的那段),但还返回了第二段,&lt;p&gt;标签包含了我们开始查找的&lt;a&gt;标签.不要惊讶,这段代码的功能是查找所有出现在指定&lt;a&gt;标签之前的&lt;p&gt;标签,因为这个&lt;p&gt;标签包含了开始的&lt;a&gt;标签,所以&lt;p&gt;标签一定是在&lt;a&gt;之前出现的.</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">CSS选择器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup支持大部分的CSS选择器 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id87\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[6]</a> ,在 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.select()</span></tt> 方法中传入字符串参数,即可使用CSS选择器的语法找到tag:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p nth-of-type(3)&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过tag标签逐层查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;body a&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot;  id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html head title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">找到某个tag标签下的直接子标签 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id87\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[6]</a> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;head &gt; title&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;title&gt;The Dormouse's story&lt;/title&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p &gt; a&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot;  id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p &gt; a:nth-of-type(2)&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;p &gt; #link1&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;body &gt; a&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># []</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">找到兄弟节点标签:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;#link1 ~ .sister&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot;  id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;#link1 + .sister&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过CSS的类名查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;.sister&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;[class~=sister]&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过tag的id查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;#link1&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a#link2&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过是否存在某个属性来查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a[href]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过属性的值来查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a[href=&quot;http://example.com/elsie&quot;]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a[href^=&quot;http://example.com/&quot;]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a[href$=&quot;tillie&quot;]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;]</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'a[href*=&quot;.com/el&quot;]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过语言设置来查找:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>multilingual_markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span style=\\\"color:rgb(64, 112, 160);\\\"> &lt;p lang=&quot;en&quot;&gt;Hello&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\"> &lt;p lang=&quot;en-us&quot;&gt;Howdy, y'all&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\"> &lt;p lang=&quot;en-gb&quot;&gt;Pip-pip, old fruit&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\"> &lt;p lang=&quot;fr&quot;&gt;Bonjour mes amis&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span>multilingual_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>multilingual_markup</span><span>)</span><span>multilingual_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>select</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'p[lang|=en]'</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;p lang=&quot;en&quot;&gt;Hello&lt;/p&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;p lang=&quot;en-us&quot;&gt;Howdy, y'all&lt;/p&gt;,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;p lang=&quot;en-gb&quot;&gt;Pip-pip, old fruit&lt;/p&gt;]</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">对于熟悉CSS选择器语法的人来说这是个非常方便的方法.Beautiful Soup也支持CSS选择器API,如果你仅仅需要CSS选择器的功能,那么直接使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>lxml</span></tt> 也可以,而且速度更快,支持更多的CSS选择器语法,但Beautiful Soup整合了CSS选择器的语法和自身方便使用API.</p></div></div><div><h1 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; color: rgb(32, 67, 92); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 0px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 200%;\\\">修改文档树<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id40\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树</p><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">修改tag的名称和属性<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id41\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">在 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#attributes\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Attributes</a> 的章节中已经介绍过这个功能,但是再看一遍也无妨. 重命名一个tag,改变属性的值,添加或删除属性:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;b class=&quot;boldest&quot;&gt;Extremely bold&lt;/b&gt;'</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span>\\\n\\\n<span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>name</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;blockquote&quot;</span><span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'verybold'</span><span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'id'</span><span>]</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(32, 128, 80);\\\">1</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote class=&quot;verybold&quot; id=&quot;1&quot;&gt;Extremely bold&lt;/blockquote&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">del</span> <span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'class'</span><span>]</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">del</span> <span>tag</span><span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">'id'</span><span>]</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;blockquote&gt;Extremely bold&lt;/blockquote&gt;</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">修改 .string<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id42\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">给tag的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 属性赋值,就相当于用当前的内容替代了原来的内容:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span>\\\n\\\n<span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;New link text.&quot;</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;New link text.&lt;/a&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">注意: 如果当前的tag包含了其它tag,那么给它的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.string</span></tt> 属性赋值会覆盖掉原有的所有内容包括子tag</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">append()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#append\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.append()</span></tt> 方法想tag中添加内容,就好像Python的列表的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.append()</span></tt> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;Foo&lt;/a&gt;&quot;</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>append</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Bar&quot;</span><span>)</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;a&gt;FooBar&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'Foo', u'Bar']</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">BeautifulSoup.new_string() 和 .new_tag()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#beautifulsoup-new-string-new-tag\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">如果想添加一段文本内容到文档中也没问题,可以调用Python的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>append()</span></tt> 方法或调用工厂方法 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup.new_string()</span></tt> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;b&gt;&lt;/b&gt;&quot;</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>append</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Hello&quot;</span><span>)</span><span>new_string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_string</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot; there&quot;</span><span>)</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>append</span><span>(</span><span>new_string</span><span>)</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;Hello there.&lt;/b&gt;</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'Hello', u' there']</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果想要创建一段注释,或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>NavigableString</span></tt> 的任何子类,将子类作为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>new_string()</span></tt> 方法的第二个参数传入:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>Comment</span><span>new_comment</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_string</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Nice to see you.&quot;</span><span>,</span> <span>Comment</span><span>)</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>append</span><span>(</span><span>new_comment</span><span>)</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;Hello there&lt;!--Nice to see you.--&gt;&lt;/b&gt;</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'Hello', u' there', u'Nice to see you.']</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"># 这是Beautiful Soup 4.2.1 中新增的方法</p><p style=\\\"text-align:justify;line-height:130%;\\\">创建一个tag最好的方法是调用工厂方法 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup.new_tag()</span></tt> :</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;b&gt;&lt;/b&gt;&quot;</span><span>)</span><span>original_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span>\\\n\\\n<span>new_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_tag</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>,</span> <span>href</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;http://www.example.com&quot;</span><span>)</span><span>original_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>append</span><span>(</span><span>new_tag</span><span>)</span><span>original_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&lt;a href=&quot;http://www.example.com&quot;&gt;&lt;/a&gt;&lt;/b&gt;</span>\\\n\\\n<span>new_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Link text.&quot;</span><span>original_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&lt;a href=&quot;http://www.example.com&quot;&gt;Link text.&lt;/a&gt;&lt;/b&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">第一个参数作为tag的name,是必填,其它参数选填</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">insert()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#insert\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.insert()</span></tt> 方法与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.append()</span></tt> 方法类似,区别是不会把新元素添加到父节点 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contents</span></tt> 属性的最后,而是把元素插入到指定的位置.与Python列表总的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.insert()</span></tt> 方法的用法下同:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>insert</span><span>(</span><span style=\\\"color:rgb(32, 128, 80);\\\">1</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;but did not endorse &quot;</span><span>)</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;I linked to but did not endorse &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'I linked to ', u'but did not endorse', &lt;i&gt;example.com&lt;/i&gt;]</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">insert_before() 和 insert_after()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#insert-before-insert-after\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>insert_before()</span></tt> 方法在当前tag或文本节点前插入内容:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;b&gt;stop&lt;/b&gt;&quot;</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_tag</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;i&quot;</span><span>)</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Don't&quot;</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>insert_before</span><span>(</span><span>tag</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&lt;i&gt;Don't&lt;/i&gt;stop&lt;/b&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>insert_after()</span></tt> 方法在当前tag或文本节点后插入内容:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>insert_after</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_string</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot; ever &quot;</span><span>))</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&lt;i&gt;Don't&lt;/i&gt; ever stop&lt;/b&gt;</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>contents</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [&lt;i&gt;Don't&lt;/i&gt;, u' ever ', u'stop']</span></pre></div></div><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\"><font color=\\\"#FF0000\\\">clear()</font><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#clear\\\" style=\\\"color: rgb(198, 15, 15); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.clear()</span></tt> 方法移除当前tag的内容:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>clear</span><span>()</span><span>tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;&lt;/a&gt;</span></pre></div></div></div><div><h2 style=\\\"font-family: 'Trebuchet MS', sans-serif; font-weight: normal; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); margin: 20px -20px 10px; padding: 3px 0px 3px 10px; background-color: rgb(242, 242, 242); font-size: 160%;\\\"><font color=\\\"#FF0000\\\">extract()</font><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#extract\\\" style=\\\"color: rgb(198, 15, 15); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>PageElement.extract()</span></tt> 方法将当前tag移除文档树,并作为方法结果返回:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>a_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>i_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>extract</span><span>()</span>\\\n\\\n<span>a_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;I linked to&lt;/a&gt;</span>\\\n\\\n<span>i_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;i&gt;example.com&lt;/i&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>i_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);\\\">None</span></pre></div></div><p style=\\\"color: rgb(0, 0, 0); text-align: justify; line-height: 130%;\\\">这个方法实际上产生了2个文档树: 一个是用来解析原始文档的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象,另一个是被移除并且返回的tag.被移除并返回的tag可以继续调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>extract</span></tt> 方法:</p><div style=\\\"color: rgb(0, 0, 0);\\\"><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>my_string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>i_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>extract</span><span>()</span><span>my_string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'example.com'</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>my_string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parent</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># None</span><span>i_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;i&gt;&lt;/i&gt;</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">decompose()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#decompose\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.decompose()</span></tt> 方法将当前节点移除文档树并完全销毁:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>a_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>decompose</span><span>()</span>\\\n\\\n<span>a_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;I linked to&lt;/a&gt;</span></pre></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">replace_with()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#replace-with\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>PageElement.replace_with()</span></tt> 方法移除文档树中的某段内容,并用新tag或文本节点替代它:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>a_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>new_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_tag</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;b&quot;</span><span>)</span><span>new_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;example.net&quot;</span><span>a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>replace_with</span><span>(</span><span>new_tag</span><span>)</span>\\\n\\\n<span>a_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;b&gt;example.net&lt;/b&gt;&lt;/a&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>replace_with()</span></tt> 方法返回被替代的tag或文本节点,可以用来浏览或添加到文档树其它地方</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">wrap()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#wrap\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>PageElement.wrap()</span></tt> 方法可以对指定的tag元素进行包装 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id89\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[8]</a> ,并返回包装后的结果:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;p&gt;I wish I was bold.&lt;/p&gt;&quot;</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>wrap</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_tag</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;b&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;I wish I was bold.&lt;/b&gt;</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>wrap</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>new_tag</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;div&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;div&gt;&lt;p&gt;&lt;b&gt;I wish I was bold.&lt;/b&gt;&lt;/p&gt;&lt;/div&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">该方法在 Beautiful Soup 4.0.5 中添加</p></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">unwrap()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unwrap\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.unwrap()</span></tt> 方法与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>wrap()</span></tt> 方法相反.将移除tag内的所有tag标签,该方法常被用来进行标记的解包:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>a_tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span>\\\n\\\n<span>a_tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unwrap</span><span>()</span><span>a_tag</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;I linked to example.com&lt;/a&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>replace_with()</span></tt> 方法相同, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>unwrap()</span></tt> 方法返回被移除的tag</p></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">输出<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id44\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">格式化输出<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id45\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>prettify()</span></tt> 方法将Beautiful Soup的文档树格式化后以Unicode编码输出,每个XML/HTML标签都独占一行</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>()</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '&lt;html&gt;\\\\\n &lt;head&gt;\\\\\n &lt;/head&gt;\\\\\n &lt;body&gt;\\\\\n  &lt;a href=&quot;http://example.com/&quot;&gt;\\\\\n...'</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;head&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/head&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;a href=&quot;http://example.com/&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    I linked to</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;i&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#     example.com</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    &lt;/i&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;/a&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象和它的tag节点都可以调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>prettify()</span></tt> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a href=&quot;http://example.com/&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  I linked to</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;i&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   example.com</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/i&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/a&gt;</span></pre></div></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">压缩输出<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id46\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">如果只想得到结果字符串,不重视格式,那么可以对一个 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag</span></tt> 对象使用Python的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>unicode()</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>str()</span></tt> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);\\\">str</span><span>(</span><span>soup</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;'</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);\\\">unicode</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>a</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;a href=&quot;http://example.com/&quot;&gt;I linked to &lt;i&gt;example.com&lt;/i&gt;&lt;/a&gt;'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>str()</span></tt> 方法返回UTF-8编码的字符串,可以指定 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id51\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码</a> 的设置.</p><p style=\\\"text-align:justify;line-height:130%;\\\">还可以调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>encode()</span></tt> 方法获得字节码或调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>decode()</span></tt> 方法获得Unicode.</p></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">输出格式<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id47\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup输出是会将HTML中的特殊字符转换成Unicode,比如“&amp;lquot;”:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&amp;ldquo;Dammit!&amp;rdquo; he said.&quot;</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);\\\">unicode</span><span>(</span><span>soup</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;\\\\u201cDammit!\\\\u201d he said.&lt;/body&gt;&lt;/html&gt;'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果将文档转换成字符串,Unicode编码会被编码成UTF-8.这样就无法正确显示HTML特殊字符了:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);\\\">str</span><span>(</span><span>soup</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;\\\\xe2\\\\x80\\\\x9cDammit!\\\\xe2\\\\x80\\\\x9d he said.&lt;/body&gt;&lt;/html&gt;'</span></pre></div></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">get_text()<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#get-text\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">如果只想得到tag中包含的文本内容,那么可以嗲用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>get_text()</span></tt> 方法,这个方法获取到tag中包含的所有文版内容包括子孙tag中的内容,并将结果作为Unicode字符串返回:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">'&lt;a href=&quot;http://example.com/&quot;&gt;</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">I linked to &lt;i&gt;example.com&lt;/i&gt;</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;/a&gt;'</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>get_text</span><span>()</span><span style=\\\"color:rgb(64, 112, 160);\\\">u'</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">I linked to example.com</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">'</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>i</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>get_text</span><span>()</span><span style=\\\"color:rgb(64, 112, 160);\\\">u'example.com'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">可以通过参数指定tag的文本内容的分隔符:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># soup.get_text(&quot;|&quot;)</span><span style=\\\"color:rgb(64, 112, 160);\\\">u'</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">I linked to |example.com|</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\\n</span><span style=\\\"color:rgb(64, 112, 160);\\\">'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">还可以去除获得文本内容的前后空白:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># soup.get_text(&quot;|&quot;, strip=True)</span><span style=\\\"color:rgb(64, 112, 160);\\\">u'I linked to|example.com'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">或者使用 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#strings-stripped-strings\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.stripped_strings</a> 生成器,获得文本列表后手动处理列表:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>[</span><span>text</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>text</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>stripped_strings</span><span>]</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'I linked to', u'example.com']</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">指定文档解析器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id48\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"text-align:justify;line-height:130%;\\\">如果仅是想要解析HTML文档,只要用文档创建 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象就可以了.Beautiful Soup会自动选择一个解析器来解析文档.但是还可以通过参数指定使用那种解析器来解析当前文档.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 第一个参数应该是要被解析的文档字符串或是文件句柄,第二个参数用来标识怎样解析文档.如果第二个参数为空,那么Beautiful Soup根据当前系统安装的库自动选择解析器,解析器的优先数序: lxml, html5lib, Python标准库.在下面两种条件下解析器优先顺序会变化:</p><blockquote><div><ul><li style=\\\"text-align:justify;line-height:130%;\\\">要解析的文档是什么类型: 目前支持, “html”, “xml”, 和 “html5”</li><li style=\\\"text-align:justify;line-height:130%;\\\">指定使用哪种解析器: 目前支持, “lxml”, “html5lib”, 和 “html.parser”</li></ul></div></blockquote><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装解析器</a> 章节介绍了可以使用哪种解析器,以及如何安装.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果指定的解析器没有安装,Beautiful Soup会自动选择其它方案.目前只有 lxml 解析器支持XML文档的解析,在没有安装lxml库的情况下,创建 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>beautifulsoup</span></tt> 对象时无论是否指定使用lxml,都无法得到解析后的对象</p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">解析器之间的区别<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id49\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup为不同的解析器提供了相同的接口,但解析器本身时有区别的.同一篇文档被不同的解析器解析后可能会生成不同结构的树型文档.区别最大的是HTML解析器和XML解析器,看下面片段被解析成HTML结构:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;b /&gt;&lt;/a&gt;&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;a&gt;&lt;b&gt;&lt;/b&gt;&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">因为空标签&lt;b /&gt;不符合HTML标准,所以解析器把它解析成&lt;b&gt;&lt;/b&gt;</p><p style=\\\"text-align:justify;line-height:130%;\\\">同样的文档使用XML解析如下(解析XML需要安装lxml库).注意,空标签&lt;b /&gt;依然被保留,并且文档前添加了XML头,而不是被包含在&lt;html&gt;标签内:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;b /&gt;&lt;/a&gt;&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;xml&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a&gt;&lt;b/&gt;&lt;/a&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">HTML解析器之间也有区别,如果被解析的HTML文档是标准格式,那么解析器之间没有任何差别,只是解析速度不同,结果都会返回正确的文档树.</p><p style=\\\"text-align:justify;line-height:130%;\\\">但是如果被解析文档不是标准格式,那么不同的解析器返回结果可能不同.下面例子中,使用lxml解析错误格式的文档,结果&lt;/p&gt;标签被直接忽略掉了:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;/p&gt;&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;lxml&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;&lt;body&gt;&lt;a&gt;&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">使用html5lib库解析相同文档会得到不同的结果:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;/p&gt;&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html5lib&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;a&gt;&lt;p&gt;&lt;/p&gt;&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">html5lib库没有忽略掉&lt;/p&gt;标签,而是自动补全了标签,还给文档树添加了&lt;head&gt;标签.</p><p style=\\\"text-align:justify;line-height:130%;\\\">使用pyhton内置库解析结果如下:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>BeautifulSoup</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;a&gt;&lt;/p&gt;&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html.parser&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a&gt;&lt;/a&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">与lxml <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id88\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[7]</a> 库类似的,Python内置库忽略掉了&lt;/p&gt;标签,与html5lib库不同的是标准库没有尝试创建符合标准的文档格式或将文档片段包含在&lt;body&gt;标签内,与lxml不同的是标准库甚至连&lt;html&gt;标签都没有尝试去添加.</p><p style=\\\"text-align:justify;line-height:130%;\\\">因为文档片段“&lt;a&gt;&lt;/p&gt;”是错误格式,所以以上解析方式都能算作”正确”,html5lib库使用的是HTML5的部分标准,所以最接近”正确”.不过所有解析器的结构都能够被认为是”正常”的.</p><p style=\\\"text-align:justify;line-height:130%;\\\">不同的解析器可能影响代码执行结果,如果在分发给别人的代码中使用了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> ,那么最好注明使用了哪种解析器,以减少不必要的麻烦.</p></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">编码<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id51\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"text-align:justify;line-height:130%;\\\">任何HTML或XML文档都有自己的编码方式,比如ASCII 或 UTF-8,但是使用Beautiful Soup解析后,文档都被转换成了Unicode:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;h1&gt;Sacr</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\xc3\\\\xa9</span><span style=\\\"color:rgb(64, 112, 160);\\\"> bleu!&lt;/h1&gt;&quot;</span><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>h1</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;h1&gt;Sacré bleu!&lt;/h1&gt;</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>h1</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>string</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'Sacr\\\\xe9 bleu!'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">这不是魔术(但很神奇),Beautiful Soup用了 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码自动检测</a> 子库来识别当前文档编码并转换成Unicode编码. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.original_encoding</span></tt> 属性记录了自动识别编码的结果:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>original_encoding</span><span style=\\\"color:rgb(64, 112, 160);\\\">'utf-8'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码自动检测</a> 功能大部分时候都能猜对编码格式,但有时候也会出错.有时候即使猜测正确,也是在逐个字节的遍历整个文档后才猜对的,这样很慢.如果预先知道文档编码,可以设置编码参数来减少自动检查编码出错的概率并且提高文档解析速度.在创建 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的时候设置 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>from_encoding</span></tt> 参数.</p><p style=\\\"text-align:justify;line-height:130%;\\\">下面一段文档用了ISO-8859-8编码方式,这段文档太短,结果Beautiful Soup以为文档是用ISO-8859-7编码:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\nmarkup = b&quot;&lt;h1&gt;\\\\xed\\\\xe5\\\\xec\\\\xf9&lt;/h1&gt;&quot;\\\nsoup = BeautifulSoup(markup)\\\nsoup.h1\\\n&lt;h1&gt;νεμω&lt;/h1&gt;\\\nsoup.original_encoding\\\n'ISO-8859-7'\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">通过传入 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>from_encoding</span></tt> 参数来指定编码方式:</p><div><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\">\\\nsoup = BeautifulSoup(markup, from_encoding=&quot;iso-8859-8&quot;)\\\nsoup.h1\\\n&lt;h1&gt;םולש&lt;/h1&gt;\\\nsoup.original_encoding\\\n'iso8859-8'\\\n</pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">少数情况下(通常是UTF-8编码的文档中包含了其它编码格式的文件),想获得正确的Unicode编码就不得不将文档中少数特殊编码字符替换成特殊Unicode编码,“REPLACEMENT CHARACTER” (U+FFFD, ) <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id90\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[9]</a> . 如果Beautifu Soup猜测文档编码时作了特殊字符的替换,那么Beautiful Soup会把 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contains_replacement_characters</span></tt> 属性标记为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>True</span></tt> .这样就可以知道当前文档进行Unicode编码后丢失了一部分特殊内容字符.如果文档中包含而 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.contains_replacement_characters</span></tt> 属性是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>False</span></tt> ,则表示就是文档中原来的字符,不是转码失败.</p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">输出编码<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id53\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">通过Beautiful Soup输出文档时,不管输入文档是什么编码方式,输出编码均为UTF-8编码,下面例子输入文档是Latin-1编码:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>b</span><span style=\\\"color:rgb(64, 112, 160);\\\">'''</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;html&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">  &lt;head&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">    &lt;meta content=&quot;text/html; charset=ISO-Latin-1&quot; http-equiv=&quot;Content-type&quot; /&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">  &lt;/head&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">  &lt;body&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">    &lt;p&gt;Sacr</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\xe9</span><span style=\\\"color:rgb(64, 112, 160);\\\"> bleu!&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">  &lt;/body&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;/html&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">'''</span>\\\n\\\n<span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;head&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;meta content=&quot;text/html; charset=utf-8&quot; http-equiv=&quot;Content-type&quot; /&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/head&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;p&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#    Sacré bleu!</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;/p&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;/body&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/html&gt;</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">注意,输出文档中的&lt;meta&gt;标签的编码设置已经修改成了与输出编码一致的UTF-8.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果不想用UTF-8编码输出,可以将编码方式传入 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>prettify()</span></tt> 方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;latin-1&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;html&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  &lt;head&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#   &lt;meta content=&quot;text/html; charset=latin-1&quot; http-equiv=&quot;Content-type&quot; /&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ...</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">还可以调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象或任意节点的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>encode()</span></tt> 方法,就像Python的字符串调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>encode()</span></tt> 方法一样:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;latin-1&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '&lt;p&gt;Sacr\\\\xe9 bleu!&lt;/p&gt;'</span>\\\n\\\n<span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>p</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;utf-8&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># '&lt;p&gt;Sacr\\\\xc3\\\\xa9 bleu!&lt;/p&gt;'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果文档中包含当前编码不支持的字符,那么这些字符将呗转换成一系列XML特殊字符引用,下面例子中包含了Unicode编码字符SNOWMAN:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">u&quot;&lt;b&gt;</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\N{SNOWMAN}</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;/b&gt;&quot;</span><span>snowman_soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>)</span><span>tag</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>snowman_soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>b</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">SNOWMAN字符在UTF-8编码中可以正常显示(看上去像是☃),但有些编码不支持SNOWMAN字符,比如ISO-Latin-1或ASCII,那么在这些编码中SNOWMAN字符会被转换成“&amp;#9731”:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;utf-8&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;☃&lt;/b&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span> <span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;latin-1&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&amp;#9731;&lt;/b&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span> <span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;ascii&quot;</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;b&gt;&amp;#9731;&lt;/b&gt;</span></pre></div></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">Unicode, dammit! (靠!)<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码自动检测</a> 功能可以在Beautiful Soup以外使用,检测某段未知编码时,可以使用这个方法:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>UnicodeDammit</span><span>dammit</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>UnicodeDammit</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Sacr</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\xc3\\\\xa9</span><span style=\\\"color:rgb(64, 112, 160);\\\"> bleu!&quot;</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>dammit</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Sacré bleu!</span><span>dammit</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>original_encoding</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 'utf-8'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果Python中安装了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>chardet</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>cchardet</span></tt> 那么编码检测功能的准确率将大大提高.输入的字符越多,检测结果越精确,如果事先猜测到一些可能编码,那么可以将猜测的编码作为参数,这样将优先检测这些编码:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>dammit</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>UnicodeDammit</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;Sacr</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\xe9</span><span style=\\\"color:rgb(64, 112, 160);\\\"> bleu!&quot;</span><span>,</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;latin-1&quot;</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;iso-8859-1&quot;</span><span>])</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>dammit</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Sacré bleu!</span><span>dammit</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>original_encoding</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># 'latin-1'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码自动检测</a> 功能中有2项功能是Beautiful Soup库中用不到的</p><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">智能引号<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id54\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">使用Unicode时,Beautiful Soup还会智能的把引号 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id91\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[10]</a> 转换成HTML或XML中的特殊字符:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>markup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>b</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&lt;p&gt;I just </span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\x93</span><span style=\\\"color:rgb(64, 112, 160);\\\">love</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\x94</span><span style=\\\"color:rgb(64, 112, 160);\\\"> Microsoft Word</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\x92</span><span style=\\\"color:rgb(64, 112, 160);\\\">s smart quotes&lt;/p&gt;&quot;</span>\\\n\\\n<span>UnicodeDammit</span><span>(</span><span>markup</span><span>,</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows-1252&quot;</span><span>],</span> <span>smart_quotes_to</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;p&gt;I just &amp;ldquo;love&amp;rdquo; Microsoft Word&amp;rsquo;s smart quotes&lt;/p&gt;'</span>\\\n\\\n<span>UnicodeDammit</span><span>(</span><span>markup</span><span>,</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows-1252&quot;</span><span>],</span> <span>smart_quotes_to</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;xml&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;p&gt;I just &amp;#x201C;love&amp;#x201D; Microsoft Word&amp;#x2019;s smart quotes&lt;/p&gt;'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">也可以把引号转换为ASCII码:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>UnicodeDammit</span><span>(</span><span>markup</span><span>,</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows-1252&quot;</span><span>],</span> <span>smart_quotes_to</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;ascii&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;p&gt;I just &quot;love&quot; Microsoft Word\\\\'s smart quotes&lt;/p&gt;'</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">很有用的功能,但是Beautiful Soup没有使用这种方式.默认情况下,Beautiful Soup把引号转换成Unicode:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>UnicodeDammit</span><span>(</span><span>markup</span><span>,</span> <span>[</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows-1252&quot;</span><span>])</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>unicode_markup</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># u'&lt;p&gt;I just \\\\u201clove\\\\u201d Microsoft Word\\\\u2019s smart quotes&lt;/p&gt;'</span></pre></div></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">矛盾的编码<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id56\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">有时文档的大部分都是用UTF-8,但同时还包含了Windows-1252编码的字符,就像微软的智能引号 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id91\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[10]</a> 一样.一些包含多个信息的来源网站容易出现这种情况. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit.detwingle()</span></tt> 方法可以把这类文档转换成纯UTF-8编码格式,看个简单的例子:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>snowmen</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">u&quot;</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\N{SNOWMAN}</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;</span> <span style=\\\"color:rgb(102, 102, 102);\\\">*</span> <span style=\\\"color:rgb(32, 128, 80);\\\">3</span><span>)</span><span>quote</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">u&quot;</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\N{LEFT DOUBLE QUOTATION MARK}</span><span style=\\\"color:rgb(64, 112, 160);\\\">I like snowmen!</span><span style=\\\"color:rgb(64, 112, 160);font-weight:bold;\\\">\\\\N{RIGHT DOUBLE QUOTATION MARK}</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;</span><span>)</span><span>doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>snowmen</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;utf8&quot;</span><span>)</span> <span style=\\\"color:rgb(102, 102, 102);\\\">+</span> <span>quote</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>encode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows_1252&quot;</span><span>)</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">这段文档很杂乱,snowmen是UTF-8编码,引号是Windows-1252编码,直接输出时不能同时显示snowmen和引号,因为它们编码不同:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>doc</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ☃☃☃I like snowmen!</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>doc</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>decode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;windows-1252&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># â˜ƒâ˜ƒâ˜ƒ“I like snowmen!”</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">如果对这段文档用UTF-8解码就会得到 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDecodeError</span></tt> 异常,如果用Windows-1252解码就回得到一堆乱码.幸好, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit.detwingle()</span></tt> 方法会吧这段字符串转换成UTF-8编码,允许我们同时显示出文档中的snowmen和引号:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>new_doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>UnicodeDammit</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>detwingle</span><span>(</span><span>doc</span><span>)</span><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>new_doc</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>decode</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;utf8&quot;</span><span>))</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ☃☃☃“I like snowmen!”</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit.detwingle()</span></tt> 方法只能解码包含在UTF-8编码中的Windows-1252编码内容,但这解决了最常见的一类问题.</p><p style=\\\"text-align:justify;line-height:130%;\\\">在创建 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit</span></tt> 对象前一定要先对文档调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit.detwingle()</span></tt> 确保文档的编码方式正确.如果尝试去解析一段包含Windows-1252编码的UTF-8文档,就会得到一堆乱码,比如: â˜ƒâ˜ƒâ˜ƒ“I like snowmen!”.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeDammit.detwingle()</span></tt> 方法在Beautiful Soup 4.1.0版本中新增</p></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">解析部分文档<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id58\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"text-align:justify;line-height:130%;\\\">如果仅仅因为想要查找文档中的&lt;a&gt;标签而将整片文档进行解析,实在是浪费内存和时间.最快的方法是从一开始就把&lt;a&gt;标签以外的东西都忽略掉. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 类可以定义文档的某段内容,这样搜索文档时就不必先解析整篇文档,只会解析在 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 中定义过的文档. 创建一个 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 对象并作为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>parse_only</span></tt> 参数给 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 的构造方法即可.</p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">SoupStrainer<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#soupstrainer\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 类接受与典型搜索方法相同的参数：<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#css\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">attrs</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#recursive\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">recursive</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text</a> , <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#keyword\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">**kwargs</a> 。下面举例说明三种 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 对象：</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>SoupStrainer</span>\\\n\\\n<span>only_a_tags</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>SoupStrainer</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;a&quot;</span><span>)</span>\\\n\\\n<span>only_tags_with_id_link2</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>SoupStrainer</span><span>(</span><span style=\\\"color:rgb(0, 112, 32);\\\">id</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;link2&quot;</span><span>)</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">def</span> <span style=\\\"color:rgb(6, 40, 126);\\\">is_short_string</span><span>(</span><span>string</span><span>):</span>\\\n    <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">return</span> <span style=\\\"color:rgb(0, 112, 32);\\\">len</span><span>(</span><span>string</span><span>)</span> <span style=\\\"color:rgb(102, 102, 102);\\\">&lt;</span> <span style=\\\"color:rgb(32, 128, 80);\\\">10</span>\\\n\\\n<span>only_short_strings</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>SoupStrainer</span><span>(</span><span>text</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>is_short_string</span><span>)</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">再拿“爱丽丝”文档来举例，来看看使用三种 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 对象做参数会有什么不同:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>html_doc</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse's story&lt;/title&gt;&lt;/head&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse's story&lt;/b&gt;&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;/a&gt;,</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;/a&gt; and</span><span style=\\\"color:rgb(64, 112, 160);\\\">&lt;a href=&quot;http://example.com/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;/a&gt;;</span><span style=\\\"color:rgb(64, 112, 160);\\\">and they lived at the bottom of a well.&lt;/p&gt;</span>\\\n\\\n<span style=\\\"color:rgb(64, 112, 160);\\\">&lt;p class=&quot;story&quot;&gt;...&lt;/p&gt;</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;&quot;&quot;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html.parser&quot;</span><span>,</span> <span>parse_only</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>only_a_tags</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/elsie&quot; id=&quot;link1&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  Elsie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/a&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  Lacie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/a&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/tillie&quot; id=&quot;link3&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  Tillie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/a&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html.parser&quot;</span><span>,</span> <span>parse_only</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>only_tags_with_id_link2</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;a class=&quot;sister&quot; href=&quot;http://example.com/lacie&quot; id=&quot;link2&quot;&gt;</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  Lacie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># &lt;/a&gt;</span>\\\n\\\n<span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">print</span><span>(</span><span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;html.parser&quot;</span><span>,</span> <span>parse_only</span><span style=\\\"color:rgb(102, 102, 102);\\\">=</span><span>only_short_strings</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>prettify</span><span>())</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Elsie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ,</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Lacie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># and</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Tillie</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ...</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">还可以将 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SoupStrainer</span></tt> 作为参数传入 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id24\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">搜索文档树</a> 中提到的方法.这可能不是个常用用法,所以还是提一下:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>html_doc</span><span>)</span><span>soup</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>find_all</span><span>(</span><span>only_short_strings</span><span>)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># [u'\\\\\n\\\\\n', u'\\\\\n\\\\\n', u'Elsie', u',\\\\\n', u'Lacie', u' and\\\\\n', u'Tillie',</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#  u'\\\\\n\\\\\n', u'...', u'\\\\\n']</span></pre></div></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">常见问题<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id59\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">代码诊断<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id60\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">如果想知道Beautiful Soup到底怎样处理一份文档,可以将文档传入 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>diagnose()</span></tt> 方法(Beautiful Soup 4.2.0中新增),Beautiful Soup会输出一份报告,说明不同的解析器会怎样处理这段文档,并标出当前的解析过程会使用哪种解析器:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4.diagnose</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>diagnose</span><span>data</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span style=\\\"color:rgb(0, 112, 32);\\\">open</span><span>(</span><span style=\\\"color:rgb(64, 112, 160);\\\">&quot;bad.html&quot;</span><span>)</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>read</span><span>()</span><span>diagnose</span><span>(</span><span>data</span><span>)</span>\\\n\\\n<span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Diagnostic running on Beautiful Soup 4.2.0</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Python version 2.7.3 (default, Aug  1 2012, 05:16:07)</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># I noticed that html5lib is not installed. Installing it may help.</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Found lxml version 2.3.2.0</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\">#</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Trying to parse your data with html.parser</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># Here's what html.parser did with the document:</span><span style=\\\"color:rgb(64, 128, 144);font-style:italic;\\\"># ...</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>diagnose()</span></tt> 方法的输出结果可能帮助你找到问题的原因,如果不行,还可以把结果复制出来以便寻求他人的帮助</p></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">文档解析错误<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id61\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">文档解析错误有两种.一种是崩溃,Beautiful Soup尝试解析一段文档结果却抛除了异常,通常是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>HTMLParser.HTMLParseError</span></tt> .还有一种异常情况,是Beautiful Soup解析后的文档树看起来与原来的内容相差很多.</p><p style=\\\"text-align:justify;line-height:130%;\\\">这些错误几乎都不是Beautiful Soup的原因,这不会是因为Beautiful Soup得代码写的太优秀,而是因为Beautiful Soup没有包含任何文档解析代码.异常产生自被依赖的解析器,如果解析器不能很好的解析出当前的文档,那么最好的办法是换一个解析器.更多细节查看 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装解析器</a> 章节.</p><p style=\\\"text-align:justify;line-height:130%;\\\">最常见的解析错误是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>HTMLParser.HTMLParseError:</span> <span>malformed</span> <span>start</span> <span>tag</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>HTMLParser.HTMLParseError:</span> <span>bad</span> <span>end</span> <span>tag</span></tt> .这都是由Python内置的解析器引起的,解决方法是 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装lxml或html5lib</a></p><p style=\\\"text-align:justify;line-height:130%;\\\">最常见的异常现象是当前文档找不到指定的Tag,而这个Tag光是用眼睛就足够发现的了. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 方法返回 [] ,而 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法返回 None .这是Python内置解析器的又一个问题: 解析器会跳过那些它不知道的tag.解决方法还是 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装lxml或html5lib</a></p></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">版本错误<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id62\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SyntaxError:</span> <span>Invalid</span> <span>syntax</span></tt> (异常位置在代码行: <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ROOT_TAG_NAME</span> <span>=</span> <span>u'[document]'</span></tt> ),因为Python2版本的代码没有经过迁移就在Python3中窒息感</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError:</span> <span>No</span> <span>module</span> <span>named</span> <span>HTMLParser</span></tt> 因为在Python3中执行Python2版本的Beautiful Soup</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError:</span> <span>No</span> <span>module</span> <span>named</span> <span>html.parser</span></tt> 因为在Python2中执行Python3版本的Beautiful Soup</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError:</span> <span>No</span> <span>module</span> <span>named</span> <span>BeautifulSoup</span></tt> 因为在没有安装BeautifulSoup3库的Python环境下执行代码,或忘记了BeautifulSoup4的代码需要从 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>bs4</span></tt> 包中引入</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError:</span> <span>No</span> <span>module</span> <span>named</span> <span>bs4</span></tt> 因为当前Python环境下还没有安装BeautifulSoup4</li></ul></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">解析成XML<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#xml\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">默认情况下,Beautiful Soup会将当前文档作为HTML格式解析,如果要解析XML文档,要在 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 构造方法中加入第二个参数 “xml”:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span>soup</span> <span style=\\\"color:rgb(102, 102, 102);\\\">=</span> <span>BeautifulSoup</span><span>(</span><span>markup</span><span>,</span> <span style=\\\"color:rgb(64, 112, 160);\\\">&quot;xml&quot;</span><span>)</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">当然,还需要 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装lxml</a></p></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">解析器的错误<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id63\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><ul><li style=\\\"text-align:justify;line-height:130%;\\\">如果同样的代码在不同环境下结果不同,可能是因为两个环境下使用不同的解析器造成的.例如这个环境中安装了lxml,而另一个环境中只有html5lib, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id49\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">解析器之间的区别</a> 中说明了原因.修复方法是在 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 的构造方法中中指定解析器</li><li style=\\\"text-align:justify;line-height:130%;\\\">因为HTML标签是 <a href=\\\"http://www.w3.org/TR/html5/syntax.html#syntax\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">大小写敏感</a> 的,所以3种解析器再出来文档时都将tag和属性转换成小写.例如文档中的 &lt;TAG&gt;&lt;/TAG&gt; 会被转换为 &lt;tag&gt;&lt;/tag&gt; .如果想要保留tag的大写的话,那么应该将文档 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#xml\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">解析成XML</a> .</li></ul></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">杂项错误<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id65\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeEncodeError:</span> <span>'charmap'</span> <span>codec</span> <span>can't</span> <span>encode</span> <span>character</span> <span>u'\\\\xfoo'</span> <span>in</span> <span>position</span> <span>bar</span></tt> (或其它类型的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>UnicodeEncodeError</span></tt> )的错误,主要是两方面的错误(都不是Beautiful Soup的原因),第一种是正在使用的终端(console)无法显示部分Unicode,参考 <a href=\\\"http://wiki.python.org/moin/PrintFails\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Python wiki</a> ,第二种是向文件写入时,被写入文件不支持部分Unicode,这时只要用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>u.encode(&quot;utf8&quot;)</span></tt> 方法将编码转换为UTF-8.</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>KeyError:</span> <span>[attr]</span></tt> 因为调用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>tag['attr']</span></tt> 方法而引起,因为这个tag没有定义该属性.出错最多的是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>KeyError:</span> <span>'href'</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>KeyError:</span> <span>'class'</span></tt> .如果不确定某个属性是否存在时,用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>tag.get('attr')</span></tt> 方法去获取它,跟获取Python字典的key一样</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>AttributeError:</span> <span>'ResultSet'</span> <span>object</span> <span>has</span> <span>no</span> <span>attribute</span> <span>'foo'</span></tt> 错误通常是因为把 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all()</span></tt> 的返回结果当作一个tag或文本节点使用,实际上返回结果是一个列表或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ResultSet</span></tt> 对象的字符串,需要对结果进行循环才能得到每个节点的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.foo</span></tt> 属性.或者使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法仅获取到一个节点</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>AttributeError:</span> <span>'NoneType'</span> <span>object</span> <span>has</span> <span>no</span> <span>attribute</span> <span>'foo'</span></tt> 这个错误通常是在调用了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法后直节点取某个属性 .foo 但是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 方法并没有找到任何结果,所以它的返回值是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> .需要找出为什么 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find()</span></tt> 的返回值是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> .</li></ul></div><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">如何提高效率<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id66\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup对文档的解析速度不会比它所依赖的解析器更快,如果对计算时间要求很高或者计算机的时间比程序员的时间更值钱,那么就应该直接使用 <a href=\\\"http://lxml.de/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">lxml</a> .</p><p style=\\\"text-align:justify;line-height:130%;\\\">换句话说,还有提高Beautiful Soup效率的办法,使用lxml作为解析器.Beautiful Soup用lxml做解析器比用html5lib或Python内置解析器速度快很多.</p><p style=\\\"text-align:justify;line-height:130%;\\\">安装 <a href=\\\"http://pypi.python.org/pypi/cchardet/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">cchardet</a> 后文档的解码的编码检测会速度更快</p><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id58\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">解析部分文档</a> 不会节省多少解析时间,但是会节省很多内存,并且搜索时也会变得更快.</p></div></div><div style=\\\"color: rgb(0, 0, 0);\\\"><h1 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);margin-top:0px;font-size:200%;\\\">Beautiful Soup 3<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#beautiful-soup-3\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h1><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup 3是上一个发布版本,目前已经停止维护.Beautiful Soup 3库目前已经被几个主要的linux平台添加到源里:</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>apt-get</span> <span>install</span> <span>Python-beautifulsoup</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">在PyPi中分发的包名字是 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> :</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>easy_install</span> <span>BeautifulSoup</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>$</span> <span>pip</span> <span>install</span> <span>BeautifulSoup</span></tt></p><p style=\\\"text-align:justify;line-height:130%;\\\">或通过 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/download/3.x/BeautifulSoup-3.2.0.tar.gz\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Beautiful Soup 3.2.0源码包</a> 安装</p><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup 3的在线文档查看 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">这里</a> ,当然还有 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">中文版</a> ,然后再读本片文档,来对比Beautiful Soup 4中有什新变化.</p><div><h2 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:160%;\\\">迁移到BS4<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id70\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h2><p style=\\\"text-align:justify;line-height:130%;\\\">只要一个小变动就能让大部分的Beautiful Soup 3代码使用Beautiful Soup 4的库和方法—-修改 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 对象的引入方式:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">BeautifulSoup</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>BeautifulSoup</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">修改为:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">from</span> <span style=\\\"color:rgb(14, 132, 181);font-weight:bold;\\\">bs4</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">import</span> <span>BeautifulSoup</span></pre></div><ul><li style=\\\"text-align:justify;line-height:130%;\\\">如果代码抛出 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError</span></tt> 异常“No module named BeautifulSoup”,原因可能是尝试执行Beautiful Soup 3,但环境中只安装了Beautiful Soup 4库</li><li style=\\\"text-align:justify;line-height:130%;\\\">如果代码跑出 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ImportError</span></tt> 异常“No module named bs4”,原因可能是尝试运行Beautiful Soup 4的代码,但环境中只安装了Beautiful Soup 3.</li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">虽然BS4兼容绝大部分BS3的功能,但BS3中的大部分方法已经不推荐使用了,就方法按照 <a href=\\\"http://www.python.org/dev/peps/pep-0008/\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">PEP8标准</a> 重新定义了方法名.很多方法都重新定义了方法名,但只有少数几个方法没有向下兼容.</p><p style=\\\"text-align:justify;line-height:130%;\\\">上述内容就是BS3迁移到BS4的注意事项</p><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">需要的解析器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id71\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup 3曾使用Python的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SGMLParser</span></tt> 解析器,这个模块在Python3中已经被移除了.Beautiful Soup 4默认使用系统的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>html.parser</span></tt> ,也可以使用lxml或html5lib扩展库代替.查看 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id9\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">安装解析器</a> 章节</p><p style=\\\"text-align:justify;line-height:130%;\\\">因为 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>html.parser</span></tt> 解析器与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SGMLParser</span></tt> 解析器不同,它们在处理格式不正确的文档时也会产生不同结果.通常 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>html.parser</span></tt> 解析器会抛出异常.所以推荐安装扩展库作为解析器.有时 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>html.parser</span></tt> 解析出的文档树结构与 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>SGMLParser</span></tt> 的不同.如果发生这种情况,那么需要升级BS3来处理新的文档树.</p></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">方法名的变化<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id72\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>renderContents</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>encode_contents</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>replaceWith</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>replace_with</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>replaceWithChildren</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>unwrap</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findAll</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findAllNext</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all_next</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findAllPrevious</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_all_previous</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findNext</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findNextSibling</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next_sibling</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findNextSiblings</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_next_siblings</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findParent</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parent</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findParents</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_parents</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findPrevious</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findPreviousSibling</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous_sibling</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>findPreviousSiblings</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find_previous_siblings</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>nextSibling</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>next_sibling</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previousSibling</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previous_sibling</span></tt></li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup构造方法的参数部分也有名字变化:</p><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(parseOnlyThese=...)</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(parse_only=...)</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(fromEncoding=...)</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup(from_encoding=...)</span></tt></li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">为了适配Python3,修改了一个方法名:</p><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.has_key()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.has_attr()</span></tt></li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">修改了一个属性名,让它看起来更专业点:</p><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.isSelfClosing</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.is_empty_element</span></tt></li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">修改了下面3个属性的名字,以免雨Python保留字冲突.这些变动不是向下兼容的,如果在BS3中使用了这些属性,那么在BS4中这些代码无法执行.</p><ul><li style=\\\"text-align:justify;line-height:130%;\\\">UnicodeDammit.Unicode -&gt; UnicodeDammit.Unicode_markup``</li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.next</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.next_element</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.previous</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>Tag.previous_element</span></tt></li></ul></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">生成器<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id73\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">将下列生成器按照PEP8标准重新命名,并转换成对象的属性:</p><ul><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>childGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>children</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>nextGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>next_elements</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>nextSiblingGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>next_siblings</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previousGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previous_elements</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previousSiblingGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>previous_siblings</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>recursiveChildGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>descendants</span></tt></li><li style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>parentGenerator()</span></tt> -&gt; <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>parents</span></tt></li></ul><p style=\\\"text-align:justify;line-height:130%;\\\">所以迁移到BS4版本时要替换这些代码:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>parent</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parentGenerator</span><span>():</span>\\\n    <span style=\\\"color:rgb(102, 102, 102);\\\">...</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">替换为:</p><div style=\\\"background:rgb(238, 255, 204);\\\"><pre style=\\\"overflow-x:auto;overflow-y:hidden;padding:5px;color:rgb(51, 51, 51);line-height:120%;border-top-width:1px;border-bottom-width:1px;border-style:solid none;border-top-color:rgb(170, 204, 153);border-bottom-color:rgb(170, 204, 153);background-color:rgb(238, 255, 204);\\\"><span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">for</span> <span>parent</span> <span style=\\\"color:rgb(0, 112, 32);font-weight:bold;\\\">in</span> <span>tag</span><span style=\\\"color:rgb(102, 102, 102);\\\">.</span><span>parents</span><span>:</span>\\\n    <span style=\\\"color:rgb(102, 102, 102);\\\">...</span></pre></div><p style=\\\"text-align:justify;line-height:130%;\\\">(两种调用方法现在都能使用)</p><p style=\\\"text-align:justify;line-height:130%;\\\">BS3中有的生成器循环结束后会返回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> 然后结束.这是个bug.新版生成器不再返回 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>None</span></tt> .</p><p style=\\\"text-align:justify;line-height:130%;\\\">BS4中增加了2个新的生成器, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#strings-stripped-strings\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">.strings 和 stripped_strings</a> . <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.strings</span></tt> 生成器返回NavigableString对象, <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>.stripped_strings</span></tt> 方法返回去除前后空白的Python的string对象.</p></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">XML<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id74\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">BS4中移除了解析XML的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulStoneSoup</span></tt> 类.如果要解析一段XML文档,使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 构造方法并在第二个参数设置为“xml”.同时 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 构造方法也不再识别 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>isHTML</span></tt> 参数.</p><p style=\\\"text-align:justify;line-height:130%;\\\">Beautiful Soup处理XML空标签的方法升级了.旧版本中解析XML时必须指明哪个标签是空标签. 构造方法的 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>selfClosingTags</span></tt> 参数已经不再使用.新版Beautiful Soup将所有空标签解析为空元素,如果向空元素中添加子节点,那么这个元素就不再是空元素了.</p></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">实体<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id75\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\">HTML或XML实体都会被解析成Unicode字符,Beautiful Soup 3版本中有很多处理实体的方法,在新版中都被移除了. <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 构造方法也不再接受 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>smartQuotesTo</span></tt> 或 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>convertEntities</span></tt> 参数. <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#unicode-dammit\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">编码自动检测</a> 方法依然有 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>smart_quotes_to</span></tt> 参数,但是默认会将引号转换成Unicode.内容配置项 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>HTML_ENTITIES</span></tt> , <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>XML_ENTITIES</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>XHTML_ENTITIES</span></tt> 在新版中被移除.因为它们代表的特性已经不再被支持.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果在输出文档时想把Unicode字符转换成HTML实体,而不是输出成UTF-8编码,那就需要用到 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id47\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">输出格式</a> 的方法.</p></div><div><h3 style=\\\"font-family:'Trebuchet MS', sans-serif;font-weight:normal;color:rgb(32, 67, 92);border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(204, 204, 204);margin:20px -20px 10px;padding:3px 0px 3px 10px;background-color:rgb(242, 242, 242);font-size:140%;\\\">迁移杂项<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id76\\\" style=\\\"color:rgb(198, 15, 15);text-decoration:none;visibility:hidden;font-size:0.8em;padding:0px 4px;\\\" title=\\\"Permalink to this headline\\\">¶</a></h3><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#string\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Tag.string</a> 属性现在是一个递归操作.如果A标签只包含了一个B标签,那么A标签的.string属性值与B标签的.string属性值相同.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id12\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">多值属性</a> 比如 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>class</span></tt> 属性包含一个他们的值的列表,而不是一个字符串.这可能会影响到如何按照CSS类名哦搜索tag.</p><p style=\\\"text-align:justify;line-height:130%;\\\">如果使用 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>find*</span></tt> 方法时同时传入了 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#text\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">text 参数</a> 和 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id32\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">name 参数</a> .Beautiful Soup会搜索指定name的tag,并且这个tag的 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#string\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">Tag.string</a> 属性包含text参数的内容.结果中不会包含字符串本身.旧版本中Beautiful Soup会忽略掉tag参数,只搜索text参数.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSoup</span></tt> 构造方法不再支持 markupMassage 参数.现在由解析器负责文档的解析正确性.</p><p style=\\\"text-align:justify;line-height:130%;\\\">很少被用到的几个解析器方法在新版中被移除,比如 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>ICantBelieveItsBeautifulSoup</span></tt> 和 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>BeautifulSOAP</span></tt> .现在由解析器完全负责如何解释模糊不清的文档标记.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>prettify()</span></tt> 方法在新版中返回Unicode字符串,不再返回字节流.</p><p style=\\\"text-align:justify;line-height:130%;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">BeautifulSoup3 文档</a></p><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id3\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[1]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">BeautifulSoup的google讨论组不是很活跃,可能是因为库已经比较完善了吧,但是作者还是会很热心的尽量帮你解决问题的.</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">[2]</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><em>(<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id19\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">1</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id23\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">2</a>)</em> 文档被解析成树形结构,所以下一步解析过程应该是当前节点的子节点</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id26\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[3]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">过滤器只能作为搜索文档的参数,或者说应该叫参数类型更为贴切,原文中用了 <tt style=\\\"padding:0px 1px;font-size:0.95em;background-color:rgb(236, 240, 243);\\\"><span>filter</span></tt> 因此翻译为过滤器</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id31\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[4]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">元素参数,HTML文档中的一个tag节点,不能是文本节点</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">[5]</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><em>(<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id18\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">1</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id33\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">2</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id34\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">3</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id35\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">4</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id36\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">5</a>)</em> 采用先序遍历方式</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">[6]</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><em>(<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id38\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">1</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id39\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">2</a>)</em> CSS选择器是一种单独的文档搜索语法, 参考 <a href=\\\"http://www.w3school.com.cn/css/css_selector_type.asp\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">http://www.w3school.com.cn/css/css_selector_type.asp</a></td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id50\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[7]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">原文写的是 html5lib, 译者觉得这是愿文档的一个笔误</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id43\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[8]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">wrap含有包装,打包的意思,但是这里的包装不是在外部包装而是将当前tag的内部内容包装在一个tag里.包装原来内容的新tag依然在执行 <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#wrap\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">wrap()</a> 方法的tag内</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id52\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">[9]</a></td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">文档中特殊编码字符被替换成特殊字符(通常是)的过程是Beautful Soup自动实现的,如果想要多种编码格式的文档被完全转换正确,那么,只好,预先手动处理,统一编码格式</td></tr></tbody></table><table style=\\\"font-size:inherit;font-weight:inherit;font-style:inherit;font-variant:inherit;border:0px;border-collapse:collapse;\\\"><colgroup><col></col><col></col></colgroup><tbody valign=\\\"top\\\"><tr><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\">[10]</td><td style=\\\"text-align:left;padding:1px 8px 1px 5px;border:0px;\\\"><em>(<a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id55\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">1</a>, <a href=\\\"http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id57\\\" style=\\\"color:rgb(53, 95, 124);text-decoration:none;\\\">2</a>)</em> 智能引号,常出现在microsoft的word软件中,即在某一段落中按引号出现的顺序每个引号都被自动转换为左引号,或右引号.</td></tr></tbody></table></div></div></div></div></div></div></div><br/></span>\\\n</div></body></html>\\u0000\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":362807}]"
metadata_diff: {"new":{"id":"2722157757bd406881024ee58c300128","parent_id":"f67288c3a0be4374b5d532b5dcc3aabc","latitude":"0.00000000","longitude":"0.00000000","altitude":"0.0000","author":"","source_url":"","is_todo":0,"todo_due":0,"todo_completed":0,"source":"joplin-desktop","source_application":"net.cozic.joplin-desktop","application_data":"","order":1764348816616,"markup_language":2,"is_shared":0,"share_id":"","conflict_original_id":"","master_key_id":"","user_data":"","deleted_time":0},"deleted":[]}
encryption_cipher_text: 
encryption_applied: 0
updated_time: 2025-11-28T21:03:17.503Z
created_time: 2025-11-28T21:03:17.503Z
type_: 13