【问题标题】:Having problems with urllib in Python 3Python 3 中的 urllib 有问题
【发布时间】:2016-07-24 18:55:51
【问题描述】:

正如标题所说,我遇到了 Python 3 的 urllib 模块的问题。 我的代码:

import urllib.request

WORD_URL = "http://learnpythonthehardway.org/words.txt"

for word in urllib.request.urlopen(WORD_URL).read():
    WORDS.append(word.strip())

我的错误是:

raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

但是当我在 Google 中手动访问该链接时,该链接可以正常工作。 任何帮助表示赞赏,谢谢。

【问题讨论】:

  • 我在浏览器上也得到了 404
  • 当您获得正确的 URL 时,word 将遍历每个字符,而不是每个单词,因为.read() 将返回一个包含整个内容的字符串。你会想要.splitlines()

标签: python-3.x urllib


【解决方案1】:

该 URL 确实链接到一个不存在的页面。 点击:http://learnpythonthehardway.org/words.txt

根据您的写作,我想您在 Google 搜索中输入了 URL。那不一样。

【讨论】:

    【解决方案2】:

    开关

    "http://learnpythonthehardway.org/words.txt"
    

    "http://learncodethehardway.org/words.txt"
    

    您应该有一个工作程序,但.read() 返回一个单词列表。您必须解析它们并删除一些字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2015-10-16
      • 1970-01-01
      相关资源
      最近更新 更多