urllib2中的未知url类型错误

时间:2014-10-25 05:53:50

标签: python python-2.7 beautifulsoup urllib2 ubuntu-14.04

我在SO上搜索了很多类似的问题,但没有找到与我的案例完全匹配的问题。

我正在尝试使用python 2.7下载视频

以下是我下载视频的代码

import urllib2
from bs4 import BeautifulSoup as bs


with open('video.txt','r') as f:
    last_downloaded_video = f.read()

webpage = urllib2.urlopen('http://*.net/watch/**-'+last_downloaded_video)

soup = bs(webpage)
a = []
for link in soup.find_all('a'):
    if link.has_attr('data-video-id'):
        a.append(link)

#try just with first data-video-id

id = a[0]['data-video-id']
webpage2 = urllib2.urlopen('http://*/video/play/'+id)
soup = bs(webpage2)
string = str(soup.find_all('script')[2])
print string
url = string.split(': ')[1].split(',')[0]
url = url.replace('"','')
print url
print type(url)

video = urllib2.urlopen(url).read()
filename = "video.mp4"
with open(filename,'wb') as f:
    f.write(video)

此代码提供了未知的网址类型错误。回溯是

Traceback (most recent call last):
  File "naruto.py", line 26, in <module>
    video = urllib2.urlopen(url).read()
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 427, in _open
    'unknown_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1247, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: 'http>

但是,当我将相同的url存储在变量中并尝试从终端下载时,不会显示错误。 我对这个问题很困惑。 我得到了一个类似的问题in python mailing list

1 个答案:

答案 0 :(得分:5)

如果没有看到您正在抓取的页面中的HTML,很难说,但是,URL开头的一个迷路'(单引号)字符可能是原因 - 这会导致相同的异常:

>>> import urllib2
>>> urllib2.urlopen("'http://blah.com")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "urllib2.py", line 404, in open
    response = self._open(req, data)
  File "urllib2.py", line 427, in _open
    'unknown_open', req)
  File "urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "urllib2.py", line 1249, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: 'http>

因此,请尝试清理您的网址并删除任何杂散的引号。

OP反馈后更新:

print语句的结果表明URL在URL字符串的开头和结尾有一个单引号字符。传递给urlopen()时,网址周围不应包含任何类型的任何引号。您可以使用以下命令从URL字符串中删除前导引号和尾随引号(单引号和双引号):

url = url.strip('\'"')