Beautiful Soup - diagnose() 方法



方法描述

Beautiful Soup 中的 diagnose() 方法是一個診斷套件,用於隔離常見問題。如果您難以理解 Beautiful Soup 對文件的操作,請將文件作為引數傳遞給 diagnose() 函式。報告將向您展示不同的解析器如何處理文件,並告訴您是否缺少解析器。

語法

diagnose(data)

引數

  • data − 文件字串。

返回值

diagnose() 方法列印根據所有可用解析器解析給定文件的結果。

示例

讓我們以這個簡單的文件作為我們的練習 -

<h1>Hello World
<b>Welcome</b>
<P><b>Beautiful Soup</a> <i>Tutorial</i><p>

以下程式碼對上述 HTML 指令碼執行診斷 -

markup = '''
<h1>Hello World
<b>Welcome</b>
<P><b>Beautiful Soup</a> <i>Tutorial</i><p>
'''

from bs4.diagnose import diagnose

diagnose(markup)

diagnose() 輸出以顯示所有可用解析器的訊息開頭 -

Diagnostic running on Beautiful Soup 4.12.2
Python version 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)]
Found lxml version 4.9.2.0
Found html5lib version 1.1

如果要診斷的文件是一個完美的 HTML 文件,則所有解析器的結果幾乎相同。但是,在我們的示例中,有很多錯誤。

首先使用內建的 html.parser。報告如下所示 -

Trying to parse your markup with html.parser
Here's what html.parser did with the markup:
   <h1>
      Hello World
   <b>
      Welcome
   </b>
   <p>
      <b>
         Beautiful Soup
         <i>
            Tutorial
         </i>
         <p>
         </p>
      </b>
   </p>
</h1>

您可以看到 Python 的內建解析器不會插入 <html> 和 <body> 標籤。未關閉的 <h1> 標籤在末尾提供匹配的 <h1>。

html5lib 和 lxml 解析器都透過將其包裝在 <html>、<head> 和 <body> 標籤中來完成文件。

Trying to parse your markup with html5lib
Here's what html5lib did with the markup:
<html>
   <head>
   </head>
   <body>
      <h1>
         Hello World
         <b>
            Welcome
         </b>
         <p>
            <b>
               Beautiful Soup
               <i>
                  Tutorial
               </i>
            </b>
         </p>
         <p>
            <b>
            </b>
         </p>
      </h1>
   </body>
</html>

使用 lxml 解析器,請注意插入結束 </h1> 的位置。此外,不完整的 <b> 標籤已得到糾正,並且已刪除懸空的 </a>。

Trying to parse your markup with lxml
Here's what lxml did with the markup:
<html>
   <body>
      <h1>
         Hello World
         <b>
            Welcome
         </b>
      </h1>
      <p>
         <b>
            Beautiful Soup
            <i>
               Tutorial
            </i>
         </b>
      </p>
      <p>
      </p>
   </body>
</html>

diagnose() 方法也以 XML 文件的形式解析文件,這在我們的例子中可能多餘。

Trying to parse your markup with lxml-xml
Here's what lxml-xml did with the markup:
<?xml version="1.0" encoding="utf-8"?>
<h1>
   Hello World
   <b>
      Welcome
   </b>
   <P>
      <b>
         Beautiful Soup
      </b>
      <i>
         Tutorial
      </i>
   <p/>
   </P>
</h1>

讓我們向 diagnose() 方法提供 XML 文件而不是 HTML 文件。

<?xml version="1.0" ?>
   <books>
      <book>
         <title>Python</title>
         <author>TutorialsPoint</author>
         <price>400</price>
      </book>
   </books>

現在,如果我們執行診斷,即使它是 XML,也會應用 html 解析器。

Trying to parse your markup with html.parser

Warning (from warnings module):
  File "C:\Users\mlath\OneDrive\Documents\Feb23 onwards\BeautifulSoup\Lib\site-packages\bs4\builder\__init__.py", line 545
    warnings.warn(
XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.

使用 html.parser,會顯示警告訊息。使用 html5lib,包含 XML 版本資訊的第 1 行被註釋掉,其餘文件被解析為 HTML 文件。

Trying to parse your markup with html5lib
Here's what html5lib did with the markup:
<!--?xml version="1.0" ?-->
<html>
   <head>
   </head>
   <body>
      <books>
         <book>
            <title>
               Python
            </title>
            <author>
               TutorialsPoint
            </author>
            <price>
               400
            </price>
         </book>
      </books>
   </body>
</html>

lxml html 解析器不會插入註釋,而是將其解析為 HTML。

Trying to parse your markup with lxml
Here's what lxml did with the markup:
<?xml version="1.0" ?>
<html>
   <body>
      <books>
         <book>
            <title>
               Python
            </title>
            <author>
               TutorialsPoint
            </author>
            <price>
               400
            </price>
         </book>
      </books>
   </body>
</html>

lxml-xml 解析器將文件解析為 XML。

Trying to parse your markup with lxml-xml
Here's what lxml-xml did with the markup:
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" ?>
   <books>
      <book>
         <title>
            Python
         </title>
         <author>
            TutorialsPoint
         </author>
         <price>
            400
         </price>
      </book>
   </books>

診斷報告可能有助於查詢 HTML/XML 文件中的錯誤。

廣告