ValueError: unknown url type

Jamus picture Jamus · Nov 30, 2012 · Viewed 17.3k times · Source

The title pretty much says it all. Here's my code:

from urllib2 import urlopen as getpage
print = getpage("www.radioreference.com/apps/audio/?ctid=5586")

and here's the traceback error I get:

Traceback (most recent call last):
  File "C:/Users/**/Dropbox/Dev/ComServ/citetest.py", line 2, in <module>
    contents = getpage("www.radioreference.com/apps/audio/?ctid=5586")
  File "C:\Python25\lib\urllib2.py", line 121, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 366, in open
    protocol = req.get_type()
  File "C:\Python25\lib\urllib2.py", line 241, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: www.radioreference.com/apps/audio/?ctid=5586

My best guess is that urllib can't retrieve data from untidy php URLs. if this is the case, is there a work around? If not, what am I doing wrong?

Answer

NlightNFotis picture NlightNFotis · Nov 30, 2012

You should first try to add 'http://' in front of the url. Also, do not store the results in print, as it is binding the reference to another (non callable) object.

So this line should be:

page_contents = getpage("http://www.radioreference.com/apps/audio/?ctid=5586")

This returns a file like object. To read its contents you need to use different file manipulation methods, like this:

for line in page_contents.readlines():
    print line