4

我正在尝试使用 beautifulsoup 来解析 html,但是每当我点击带有内联脚本标签的页面时,beautifulsoup 都会对内容进行编码,但最终不会将其解码。

这是我使用的代码:

from bs4 import BeautifulSoup

if __name__ == '__main__':

    htmlData = '<html> <head> <script type="text/javascript"> console.log("< < not able to write these & also these >> "); </script> </head> <body> <div> start of div </div> </body> </html>'
    soup = BeautifulSoup(htmlData)
    #... using BeautifulSoup ...
    print(soup.prettify() )

我想要这个输出:

<html>
 <head>
  <script type="text/javascript">
   console.log("< < not able to write these & also these >> ");
  </script>
 </head>
 <body>
  <div>
   start of div
  </div>
 </body>
</html>

但我得到这个输出:

<html>
 <head>
  <script type="text/javascript">
   console.log("&lt; &lt; not able to write these &amp; also these &gt;&gt; ");
  </script>
 </head>
 <body>
  <div>
   start of div
  </div>
 </body>
</html>
4

2 回答 2

1

您可能想尝试lxml

import lxml.html as LH

if __name__ == '__main__':
    htmlData = '<html> <head> <script type="text/javascript"> console.log("< < not able to write these & also these >> "); </script> </head> <body> <div> start of div </div> </body> </html>'
    doc = LH.fromstring(htmlData)
    print(LH.tostring(doc, pretty_print = True))

产量

<html>
<head><script type="text/javascript"> console.log("< < not able to write these & also these >> "); </script></head>
<body> <div> start of div </div> </body>
</html>
于 2012-12-02T18:37:19.080 回答
-1

你可以这样做:

htmlCodes = (
('&', '&amp;'),
('<', '&lt;'),
('>', '&gt;'),
('"', '&quot;'),
("'", '&#39;'),
)

for i in htmlCodes:
    soup.prettify().replace(i[1], i[0])
于 2012-12-02T18:23:28.653 回答