BeautifulSoup4 在 Sagemath 中

BeautifulSoup4 in Sagemath

我正在尝试使用 BS4 执行网页抓取,然后在 Sage 6.2(在 Lion 10.7.5 中)中对结果进行一些代数运算。我假设 /Applications/Sage-6.2.app/Contents/Resources/sage/sage -i beautifulsoup4 会起作用,但结果是

Attempting to download package beautifulsoup4
>>> Checking online list of optional packages.
[Traceback (most recent call last):
  File "<stdin>", line 35, in <module>
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 240, in retrieve
    fp = self.open(url, data)
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 359, in open_http
    return self.http_error(url, fp, errcode, errmsg, headers)
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 376, in http_error
    return self.http_error_default(url, fp, errcode, errmsg, headers)
  File "<stdin>", line 17, in http_error_default
IOError: [Errno 404] Not Found: '//www.sagemath.org/spkg/optional/list'
Error: failed to download http://www.sagemath.org/spkg/optional/list, aborting

接下来,我尝试 /Applications/Sage-6.2.app/Contents/Resources/sage/sage -sh <<< "easy_install --verbose pip" 通过 pip 获取 bs4。成功了;但是 /Applications/Sage-6.2.app/Contents/Resources/sage/local/bin/pip2.7 install beautifulsoup4 失败并显示错误消息

Traceback (most recent call last):
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 548, in <module>
    main()
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 530, in main
    known_paths = addusersitepackages(known_paths)
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 266, in addusersitepackages
    user_site = getusersitepackages()
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 241, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 231, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
    return get_config_vars().get(name)
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
    import re
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/re.py", line 105, in <module>
    import sre_compile
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_compile.py", line 14, in <module>
    import sre_parse
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_parse.py", line 17, in <module>
    from sre_constants import *
  File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_constants.py", line 18, in <module>
    from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT

接下来,我尝试升级到 Sage 6.6。一切顺利,bs4 的安装也是如此。但是,当我打开笔记本时,我无法对单元格进行 运行 编码,因为 "Shift-Enter" 命令仅注册为 "Enter" 并给了我一个新行而不是评估代码。此外,没有 "evaluate" 按钮。任何解决这些问题的建议都将不胜感激。

不幸的是,Sage 已经改变了它处理 BS 等可选包的方式,因此旧版本可能无法使用它们。不过,你总是可以去镜像,比如 http://mirrors.mit.edu/sage/spkg/optional/ 下载它,并将它放在你的(旧 Sage 的)可选 spkg 目录中,然后尝试 sage -i path/to/file.

至于笔记本问题,由于 jQuery 版本不同,这应该是在浏览器中执行 "hard cache reset" 的问题,参见例如here.