如何使用 python 取消缩短 URL?

编程入门 行业动态 更新时间:2024-10-16 20:28:12
本文介绍了如何使用 python 取消缩短 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经看过这个帖子 - 如何取消缩短网址?

I have seen this thread already - How can I unshorten a URL?

我对已解决答案的问题(即使用 unshort.me API)是我专注于取消缩短 youtube 链接.由于 unshort.me 很容易使用,这会返回几乎 90% 的带有验证码的结果,我无法解决.

My issue with the resolved answer (that is using the unshort.me API) is that I am focusing on unshortening youtube links. Since unshort.me is used readily, this returns almost 90% of the results with captchas which I am unable to resolve.

到目前为止,我坚持使用:

So far I am stuck with using:

def unshorten_url(url): resolvedURL = urllib2.urlopen(url) print resolvedURL.url #t = Test() #c = pycurl.Curl() #c.setopt(c.URL, 'api.unshort.me/?r=%s&t=xml' % (url)) #c.setopt(c.WRITEFUNCTION, t.body_callback) #c.perform() #c.close() #dom = xml.dom.minidom.parseString(t.contents) #resolvedURL = dom.getElementsByTagName("resolvedURL")[0].firstChild.nodeValue return resolvedURL.url

注意:评论中的所有内容都是我在使用返回验证码链接的 unshort.me 服务时尝试做的.

Note: everything in the comments is what I tried to do when using the unshort.me service which was returning captcha links.

有没有人知道一种更有效的方法来完成这个操作而不使用 open(因为它浪费带宽)?

Does anyone know of a more efficient way to complete this operation without using open (since it is a waste of bandwidth)?

推荐答案

在该问题中使用评分最高的答案(不是接受的答案):

Use the best rated answer (not the accepted answer) in that question:

# This is for Py2k. For Py3k, use http.client and urllib.parse instead, and # use // instead of / for the division import httplib import urlparse def unshorten_url(url): parsed = urlparse.urlparse(url) h = httplib.HTTPConnection(parsedloc) 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

更多推荐

如何使用 python 取消缩短 URL?

本文发布于:2023-11-28 21:20:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   python   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!