简体   繁体   English

网站上的“IOError: [Errno 2] 没有这样的文件或目录”——Python

[英]'IOError: [Errno 2] No such file or directory' on website -- Python

I'm doing some web scraping in Python and I'm running into a strange error.我正在用 Python 进行一些网页抓取,但遇到了一个奇怪的错误。 Sometimes when I try to open and read an html file using a URL I get the following error:有时,当我尝试使用 URL 打开和读取 html 文件时,会出现以下错误:

'IOError: [Errno 2] No such file or directory'

The code that is producing this error is here: Terminal doesn't like the last line in this block of code.产生此错误的代码在这里:终端不喜欢此代码块中的最后一行。

for game in games:
            url = game
            page = urllib.urlopen(url).read()

The list games is a list of urls that definitely exist.列表games是肯定存在的网址列表。 This is what games looks like when I print it out:这是我打印出来的games样子:

['nba.com/games/20160323/ATLWAS/gameinfo.html', 
'nba.com/games/20160323/MILCLE/gameinfo.html',
'nba.com/games/20160323/TORBOS/gameinfo.html',
'nba.com/games/20160323/ORLDET/gameinfo.html',
'nba.com/games/20160323/NYKCHI/gameinfo.html', 
'nba.com/games/20160323/UTAHOU/gameinfo.html', 
'nba.com/games/20160323/SACMIN/gameinfo.html', 
'nba.com/games/20160323/MIASAS/gameinfo.html', 
'nba.com/games/20160323/PHIDEN/gameinfo.html', 
'nba.com/games/20160323/LALPHX/gameinfo.html', 
'nba.com/games/20160323/DALPOR/gameinfo.html', 
'nba.com/games/20160323/LACGSW/gameinfo.html']

Does anyone know why I'm getting this error?有谁知道我为什么会收到这个错误? I actually got this error once before, and although I solved it on that occasion, I didn't understand why what I did worked so I wanted to ask the community.实际上我之前也遇到过这个错误,虽然我在那次解决了它,但我不明白我所做的为什么有效,所以我想问问社区。 Previously, I had games set up so that instead of having the full URL, it only had part of it.以前,我对games设置,因此它没有完整的 URL,而只有其中的一部分。

For example, the first element in the old games was /games/20160323/ATLWAS/gameinfo.html .例如,旧games的第一个元素是/games/20160323/ATLWAS/gameinfo.html Then in the for loop shown above, instead of url = game , I wrote nba.com+url .然后在上面显示的 for 循环中,我写了nba.com+url而不是url = game When I ran it with the changes outlined above, it ran as expected.当我使用上述更改运行它时,它按预期运行。 Any help would be appreciated.任何帮助,将不胜感激。

Python 2 :蟒蛇2

for game in games:
        url = "http://" + game
        page = urllib.urlopen(url).read()

(For Python 3 you have to include .request) (对于Python 3,您必须包含 .request)

page = urllib.request.urlopen(url).read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM