bra*_*mat 6 python youtube curl urllib hyperlink
我已经看过这个帖子了 - 我怎样才能取消URL?
我对已解决的答案(即使用unshort.me API)的问题在于我专注于改进youtube链接.由于unshort.me很容易使用,因此使用验证码返回了近90%的结果,我无法解决.
到目前为止,我一直坚持使用:
def unshorten_url(url):
resolvedURL = urllib2.urlopen(url)
print resolvedURL.url
#t = Test()
#c = pycurl.Curl()
#c.setopt(c.URL, 'http://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
Run Code Online (Sandbox Code Playgroud)
注意:注释中的所有内容都是我在使用返回captcha链接的unshort.me服务时尝试做的.
有没有人知道一种更有效的方法来完成这个操作而不使用open(因为它浪费了带宽)?
Ped*_*iro 15
在该问题中使用评分最高的答案(不是接受的答案):
# 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(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
Run Code Online (Sandbox Code Playgroud)
ber*_*sam 13
一行功能,使用请求库,是的,它支持递归.
def unshorten_url(url):
return requests.head(url, allow_redirects=True).url
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5413 次 |
| 最近记录: |