Python urllib2.URLError:<;url打开错误未知url类型:webcal>;尝试写入.ics文件时
我正在尝试将日历(.ics)的内容从网页(如)写入文件。下面是我的代码尝试:Python urllib2.URLError:<;url打开错误未知url类型:webcal>;尝试写入.ics文件时,python,html,Python,Html,我正在尝试将日历(.ics)的内容从网页(如)写入文件。下面是我的代码尝试: def write_file(ics_url, set_calendar_name): url = ics_url response = urllib2.urlopen(url) webContent = response.read() f = open(set_calendar_name, 'w') f.write(webContent) f.close
def write_file(ics_url, set_calendar_name):
url = ics_url
response = urllib2.urlopen(url)
webContent = response.read()
f = open(set_calendar_name, 'w')
f.write(webContent)
f.close
print 'File saved as: ' + set_calendar_name
print
return set_calendar_name
但是,我不断遇到以下错误:
Traceback (most recent call last):
File "get_calendar.py", line 99, in <module>
write_file(get_ics_url('https://careers.unc.edu/calendar'), 'carolina.ics')
File "get_calendar.py", line 36, in write_file
response = urllib2.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 452, in _open
'unknown_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1266, in unknown_open
raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: webcal>
回溯(最近一次呼叫最后一次):
文件“get_calendar.py”,第99行,在
写入文件(获取ics url('https://careers.unc.edu/calendar),“carolina.ics”
文件“get_calendar.py”,第36行,在write_文件中
response=urlib2.urlopen(url)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,urlopen中的第154行
返回opener.open(url、数据、超时)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第429行,打开
响应=自身打开(请求,数据)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第452行,打开
“未知_打开”,请求)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第407行,在调用链中
结果=func(*args)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第1266行,未知打开
引发url错误('未知url类型:%s'%1!'
urllib2.URLError:
有没有人知道是什么原因导致了我的代码?例如,当我使用以下URL运行程序时,我的程序运行得很好:但没有使用前面提到的URL 解决方案:我最终发现了问题所在。
link.get('href')
包含
'webcal:'
而不是
'https:'
一开始,我不得不更换它。固定代码应为:
if link.get('href') != None and '.ics' in link.get('href'):
endout = link.get('href')
if endout[:6] == 'webcal':
endout ='https' + endout[6:]
解决方案:我最终发现了问题所在。
link.get('href')
包含
'webcal:'
而不是
'https:'
一开始,我不得不更换它。固定代码应为:
if link.get('href') != None and '.ics' in link.get('href'):
endout = link.get('href')
if endout[:6] == 'webcal':
endout ='https' + endout[6:]