Python urlopen 错误

标签 python python-2.5 urlopen

我刚买了 synology NAS (DS213J),我想在上面运行 python 脚本。

我的 python 脚本:

  1 #!/opt/bin/python
  2
  3 import urllib
  4 response = urllib.urlopen('http://google.com')
  5 html = response.read()
  6 print html

当我运行这个脚本时,我得到了这个输出:

Traceback (most recent call last):
  File "/opt/bin/test.py", line 4, in <module>
    response = urllib.urlopen('http://google.com')
  File "/opt/lib/python2.5/urllib.py", line 82, in urlopen
    return opener.open(url)
  File "/opt/lib/python2.5/urllib.py", line 190, in open
    return getattr(self, name)(url)
  File "/opt/lib/python2.5/urllib.py", line 272, in open_http
    import httplib
  File "/opt/lib/python2.5/httplib.py", line 70, in <module>
    import mimetools
  File "/opt/lib/python2.5/mimetools.py", line 6, in <module>
    import tempfile
  File "/opt/lib/python2.5/tempfile.py", line 33, in <module>
    from random import Random as _Random
  File "/opt/lib/python2.5/random.py", line 58, in <module>
    SG_MAGICCONST = 1.0 + _log(4.5)
OverflowError: math range error

我也试过使用 urllib2 但没有成功。

脚本:

  1 #!/opt/bin/python
  2
  3 import urllib2
  4 response = urllib2.urlopen('http://google.com')
  5 html = response.read()
  6 print html

控制台输出:

Traceback (most recent call last):
  File "/opt/bin/test.py", line 3, in <module>
    import urllib2
  File "/opt/lib/python2.5/urllib2.py", line 92, in <module>
    import httplib
  File "/opt/lib/python2.5/httplib.py", line 70, in <module>
    import mimetools
  File "/opt/lib/python2.5/mimetools.py", line 6, in <module>
    import tempfile
  File "/opt/lib/python2.5/tempfile.py", line 33, in <module>
    from random import Random as _Random
  File "/opt/lib/python2.5/random.py", line 58, in <module>
    SG_MAGICCONST = 1.0 + _log(4.5)
OverflowError: math range error

我不知道这些错误是什么意思;我用谷歌搜索了一些但没有成功。上面的脚本是下载电影字幕的更大脚本的一部分(我刚刚从更大的脚本中提取错误部分并发布在这里)。

我写道这个脚本在 synology DS213j 上运行,因为我认为这可能与 python 安装有关。通常我在为我的 synylogy 安装 ipkg 时遇到问题。我结束了 this教程。从教程中安装 Bootstrap 后,我只需运行 ipkg install python 即可成功安装包。我的 python 版本是 Python 2.5.6

谢谢

最佳答案

问题出在#!/opt/bin/python,运行which python 找出你的python 二进制完整路径是什么。

如您所见,您的列表没问题:

>>> import urllib
>>> response = urllib.urlopen('http://google.com')
>>> html = response.read()
>>> print html
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="iw" dir="rtl"><head><meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title>[...]</body></html>

我认为你应该使用 python 2.7follow

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.7

使用ipkg:

ipkg update
ipkg install python27

python2.7 将启动 python 解释器。

关于Python urlopen 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711905/

相关文章:

python - 从 qComboBox 弹出菜单捕获鼠标按钮按下信号

python - 使用python将字符串转换为字典

python - 判断 Python 是否处于交互模式

python - 告诉 urllib2 使用自定义 DNS

python - 如何返回到 Urlopen 对象中的第一行

python - Selenium Phantomjs 浏览器在启动时挂起。我该如何调试它?

python - 如何在云端部署python scraper?

python - urllib.request.urlopen 超时没有互联网

python - 来自 TensorFlow 的预训练 NN 的超参数调整

python - 重新运行程序时出现问题