2017-07-10 2 views
1

Webページ(例:https://careers.unc.edu/calendar)からカレンダーの内容(.ics)をファイルに書き込もうとしています。以下は、私のコードの試みです:urllib2.URLError:<urlopenエラーの不明なURLタイプ:webcal> .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 

    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> 

誰もが私のコードでそれを引き起こしているものを上の任意のアイデアを持っていますか?私のプログラムは、例えばURL:https://meded.hms.harvard.edu/calendarで実行したが、前に述べたものではなかった。

答えて

0

解決策:私は何が間違っていたか把握しました。

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:] 
関連する問題