在Python 3中遇到urllib问题

时间:2016-07-24 18:55:52

标签: python-3.x urllib

就像标题所说的那样,我在使用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

但是当我在谷歌手动转到它时,链接工作正常。 感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

切换

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

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

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