我使用計算器this後的代碼unshorten網址...無法解開bit.ly網址?
import httplib
import urlparse
def unshorten_url(url):
parsed = urlparse.urlparse(url)
h = httplib.HTTPConnection(parsed.netloc)
resource = parsed.path
if parsed.query != "":
resource += "?" + parsed.query
h.request('HEAD', resource)
response = h.getresponse()
if response.status/100 == 3 and response.getheader('Location'):
return unshorten_url(response.getheader('Location')) # changed to process chains of short urls
else:
return url
所有縮短鏈接bit.ly的新創建的URL unshortned「CEPT。
我得到這個錯誤:
>>> unshorten_url("bit.ly/1atTViN")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in unshorten_url
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 955, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 989, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 951, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 811, in _send_output
self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 773, in send
self.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 754, in connect
self.timeout, self.source_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 61] Connection refused
是怎麼回事?
downvote最初發布的答案,收到6 upvotes – sirvon
@ user2799617:然後downvote原來的,而不是這個問題。 –
http://www.ankitpanda.com/tweeting-with-python/,但我已經嘗試過另一個網址以及... youtube.com/watch?v=eeAjkbNq4xI – sirvon