Problem with the urllib

Asked

Viewed 138 times

3

I am trying to solve the python challege #4 but when I run my possible solution I get an error. My solution is the following:

import urllib, re


pattern = re.compile("and the next nothing is \d")
text = "and the next nothing is 12345"
nothing = int(text[-5:])
check = re.search(pattern, text)

while check:  # main loop
    url = urllib.urlopen("www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}".format(nothing))
    text = url.read()
    nothing = int(text[-5:])
    check = re.search(pattern, text)

print text

and when I run I get this error:


Traceback (most recent call last):
  File "Python_Challenge_4.py", line 12, in 
    url = urllib.urlopen("www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}".format(nothing))
  File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/usr/lib/python2.7/urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "/usr/lib/python2.7/urllib.py", line 463, in open_file
    return self.open_local_file(url)
  File "/usr/lib/python2.7/urllib.py", line 477, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: 'www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345'

Note: I don’t want a solution to python Challenge 4, I just want to know the cause of that mistake.

  • tried to put http at the beginning of the url ?

1 answer

3


If the method urlopen not identify the scheme of the URL (http:, https:, file:, etc) it interprets as opening a local file. See official documentation:

Open a network Object denoted by a URL for Reading. If the URL does not have a Scheme Identifier, or if it has file: as its Scheme Dentifier, this Opens a local file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.