BeautifulSoup soup.prettify()给出了奇怪的输出

abu*_*rak 4 python django urllib2 beautifulsoup

我正在尝试解析一个网站,我将在稍后的Django项目中使用它.为此,我使用的是urllib2和BeautifulSoup4.但是,我无法得到我想要的东西.BeautifulSoup对象的输出很奇怪.我尝试了不同的页面,它有效(输出正常).我以为是因为页面.然后,当我的朋友试图做同样的事情时,他得到了正常的输出.我无法弄清楚问题.

这是我要解析的网站.

这是命令"soup.prettify()"之后的奇怪输出的示例:

t   d       B   G   C   O   L   O   R   =   "   #   9   9   0   4   0   4   "       w   i   d   t   h   =   "   3   "   &gt;   i   m   g       S   R   C   =   "   1   p   .   g   i   f   "       A   L   T       B   O   R   D   E   R   =   "   0   "       h   e   i   g   h   t   =   "   1   "       w   i   d   t   h   =   "   3   "   &gt;   /   t   d   &gt;   \n           /   t   r   &gt;   \n           t   r   &gt;   \n                   t   d       c   o   l   s   p   a   n   =   "   3   "       B   G   C   O   L   O   R   =   "   #   9   9   0   4   0   4   "       w   i   d   t   h   =   "   6   0   0   "       h   e   i   g   h   t   =   "   3   "   &gt;   i   m   g       s   r   c   =   "   1   p   .   g   i   f   "       w   i   d   t   h   =   "   6   0   0   "   \n                   h   e   i   g   h   t   =   "   1   "   &gt;   /   t   d   &gt;   \n           /   t   r   &gt;   \n   /   t   a   b   l   e   &gt;   \n   /   c   e   n   t   e   r   &gt;   /   d   i   v   &gt;   \n   \n   p   &gt;   &amp;n   b   s   p   ;   &amp;n   b   s   p   ;   &amp;n   b   s   p   ;   &amp;n   b   s   p   ;   /   p   &gt;   \n   /   b   o   d   y   &gt;   \n   /   h   t   m   l   &gt;\n  </p>\n </body>\n</html>'
Run Code Online (Sandbox Code Playgroud)

Hoo*_*ked 6

下面是一个小例子,确实对我的工作,包括您有问题的HTML代码段.没有你的代码很难说,但我的猜测是你在' '.join(A.split())某个地方做了些什么.

import urllib2, bs4

url = "http://kafemud.bilkent.edu.tr/monu_tr.html"
req = urllib2.urlopen(url)
raw = req.read()
soup = bs4.BeautifulSoup(raw)

print soup.prettify().encode('utf-8')
Run Code Online (Sandbox Code Playgroud)

赠送:

....
<td bgcolor="#990404" width="3">
       <img alt="" border="0" src="1p.gif" width="3"/>
      </td>
      <td bgcolor="#FFFFFF" valign="TOP">
       <div align="left">
        <table align="left" border="0" cellpadding="10" cellspacing="0" valign="TOP" width="594">
         <tr>
          <td align="left" valign="top">
           <table align="left" border="0" cellpadding="0" cellspacing="0" class="icerik" width="574">
....
Run Code Online (Sandbox Code Playgroud)