【问题标题】:Parsing a specific website crashes the Python process解析特定网站会使 Python 进程崩溃
【发布时间】:2013-04-21 23:50:00
【问题描述】:

希望解析图像的 HTML 页面(来自http://www.z-img.com),当我将该页面加载到 BeautifulSoup (bs4) 中时,Python 崩溃了。 “问题详细信息”显示etree.pyd 是“故障模块名称”,这意味着它可能是一个解析错误,但到目前为止,我还不能完全确定它的原因。

这是我在 Python2.7 上可以归结为最简单的代码:

import requests, bs4

url = r"http://z-img.com/search.php?&ssg=off&size=large&q=test"
r = requests.get(url)
html = r.content
#or 
#import urllib2
#html = urllib2.urlopen(url).read()
soup  = bs4.BeautifulSoup(html)

在我通过 JsBeautifier.com 之后,连同 PasteBin (http://pastebin.com/XYT9g4Lb) 上的示例输出。

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    这是一个错误that was fixed in lxml version 2.3.5。升级到 2.3.5 或更高版本。

    【讨论】:

      【解决方案2】:

      哦,你去吧,我提交问题后的第一件事自然是解决方案:<!DOCTYPE> 标签似乎是它的根源。我创建了一个新的 HTML 文件 temp.html:

      <!DOCTYPE>
      <html>
      </html>
      

      并将其作为 HTML 字符串传递给 BeautifulSoup,这足以让 Python 再次崩溃。所以我只需要在将来将 HTML 传递给 BeautifulSoup 之前删除该标签:

      import requests, bs4
      
      url = r"http://z-img.com/search.php?&ssg=off&size=large&q=test"
      r = requests.get(url)
      html = r.content
      #or 
      #import urllib2
      #html = urllib2.urlopen(url).read()
      
      #replace the declaration with nothing, and my problems are solved
      html = html.replace(r"<!DOCTYPE>", "")
      soup  = bs4.BeautifulSoup(html)
      

      希望这可以节省其他人一些时间。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多