ValueError:未知的url类型

Jam*_*mus 6 python parsing urllib urllib2 traceback

标题基本概括了所有内容.这是我的代码:

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

这是我得到的追溯错误:

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
Run Code Online (Sandbox Code Playgroud)

我最好的猜测是urllib无法从不整洁的php URL中检索数据.如果是这种情况,是否有解决方法?如果没有,我做错了什么?

Nli*_*tis 8

您应该首先尝试'http://'在网址前添加.另外,不要存储在结果print,因为它被结合参考另一(非可调用)对象.

所以这一行应该是:

page_contents = getpage("http://www.radioreference.com/apps/audio/?ctid=5586")
Run Code Online (Sandbox Code Playgroud)

这将返回像object这样的文件.要读取其内容,您需要使用不同的文件操作方法,如下所示:

for line in page_contents.readlines():
    print line
Run Code Online (Sandbox Code Playgroud)