python - 间隔输出 beautifulsoup

标签 python python-3.x web-scraping beautifulsoup

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

 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')

关于python - 间隔输出 beautifulsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046970/

相关文章:

php - 使用 curl 从 asp.net 页面获取数据

python - 如何对一列使用透视/分组并连接其他列?

python - 在 Pandas 中定义列类型为 'list'

python - 从多个 tkinter 选项菜单中获取值

python - 多重处理 python for 循环并将结果保存为字典

python-2.7 - 在每个域上以高并发和低请求率执行 Scrapy 广泛抓取。

python - 如何知道在findContour之后是否需要反转阈值类型

python - 在 lambdify 的 sympy 表达式上使用 numba.autojit

python-3.x - Docker python 无法导入通过 apt-get 安装的模块

python - 如何将网站上的图片保存到本地文件夹