Skip to main content
 首页 » 编程设计

python之间隔输出 beautifulsoup

2025年05月04日10kevingrace

我正在尝试抓取网站内容。但是在输出中我得到了不需要的空格,因此我无法解释这个输出。我正在使用一个简单的代码:

 import urllib2 
 from bs4 import BeautifulSoup 
 html= 'http://idlebrain.com/movie/archive/index.html' 
 soup = BeautifulSoup(urllib2.urlopen(html).read()) 
 print(soup.prettify(formatter=None)) 

OUTPUT::(输出非常大,所以只有一小部分是为了了解我面临的问题)

       <html><head><title>Telugu cinema reviews by Jeevi - idlebrain.com</title>  
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> 
        </head><bodybgcolor="#FFFFFF" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0"><table border="0" cellpadding="0" cellspacing="0" width="96%"> 
    <tr> 
    <td align="left"> <img alt="Idlebrain.Com" height="63" src="../../image/vox_r01_c2.gif"width="264"/></td> 
   <td><div align="right"><script type="text/javascript"><!-- 
      g   o   o   g   l   e   _   a   d   _   c   l   i   e   n   t       =       "   c   a   -   p   u   b   -   8   8   6   3   7   1   8   7   5   2   0   4   9   7   3   9   "   ;    
 
       /   *       r   e   v   i   e   w   s   -   h   o   r       *   /    
 
       g   o   o   g   l   e   _   a   d   _   s   l   o   t       =       "   1   6   4   8   6   2   0   2   7   3   "   ;    
 
      g   o   o   g   l   e   _   a   d   _   w   i   d   t   h       =       7   2   8   ;    
 
      g   o   o   g   l   e   _   a   d   _   h   e   i   g   h   t       =       9   0   ;    
 
      /   /   -   -   >    
 
     <   /   s   c   r   i   p   t   >    
 
     <   s   c   r   i   p   t       t   y   p   e   =   "   t   e   x   t   /   j   a   v   a   s   c   r   i   p   t   "    
 
     s   r   c   =   "   h   t   t   p   :   /   /   p   a   g   e   a   d   2   .   g   o   o   g   l   e   s   y   n   d   i   c   a   t   i   o   n   .   c   o   m   /   p   a   g   e   a   d   /   s   h   o   w   _   a   d   s   .   j   s   "   >    
 
     <   /   s   c   r   i   p   t   >    
 
                           <   /   d   i   v   >    
 
                   <   /   t   d   >    
 
           <   /   t   r   >    
 
    <   /   t   a   b   l   e   >    
 
    <   t   a   b   l   e       w   i   d   t   h   =   "   9   6   %   "       b   o   r   d   e   r   =   "   0   "       c   e   l   l   s   p   a   c   i   n   g   =   "   0   "       c   e   l   l   p   a   d   d   i   n   g   =   "   0   "   >    
 
             <   t   r   >        
 
                     <   t   d       w   i   d   t   h   =   "   1   2   8   "       v   a   l   i   g   n   =   "   t   o   p   "       a   l   i   g   n   =   "   l   e   f   t   "   >        
 
                           <   t   a   b   l   e       b   o   r   d   e   r   =   "   0   "       c   e   l   l   p   a   d   d   i   n   g   =   "   0   "       c   e   l   l   s   p   a   c   i   n   g   =   "   0   "       w   i   d   t   h   =   "   1   1   9   "   >    
 
     <   /   t   r   >    
 
    <   /   t   a   b   l   e   >    
 
     <   /   b   o   d   y   >    
 
     <   /   h   t   m   l   >    
 
           </script></div></td></tr></table></body></html>  

谢谢!!!!

请您参考如下方法:

您可以将解析器指定为html.parser:

soup = BeautifulSoup(urllib2.urlopen(html).read(), 'html.parser') 

或者您可以指定 html5 解析器:

soup = BeautifulSoup(urllib2.urlopen(html).read(), 'html5') 

还没有安装html5 解析器吗?从命令行安装它:

sudo apt-get install python-html5lib 

您也可以使用 xml 解析器,但您可能会在 multi-valued attributes 中看到一些差异。像 class="foo bar":

soup = BeautifulSoup(urllib2.urlopen(html).read(), 'xml')