Python Short Url扩展器

时间:2017-08-13 18:39:01

标签: python url

我在扩展短网址时遇到问题,因为并非我所有人都使用相同的重定向:

这个想法是扩展缩短的网址:这里有一些简短的网址示例 - >最后的网址。我需要一个函数来获取缩短的url并返回扩展的URL

http://chollo.to/675za - > http://www.elcorteingles.es/limite-48-horas/equipaje/?sorting=priceAsc&aff_id=2118094&dclid=COvjy8Xrz9UCFeMi0wod4ZULuw

所以我有一些半工作的东西,它在一些abobe例子中失败了

import requests
import httplib
import urlparse


def unshorten_url(url):
try:

parsed = urlparse.urlparse(url)
h = httplib.HTTPConnection(parsed.netloc)
h.request('HEAD', parsed.path)
response = h.getresponse()

if response.status / 100 == 3 and response.getheader('Location'):
url = requests.get(response.getheader('Location')).url
print url
return url

else:
url = requests.get(url).url
print url
return url


except Exception as e:
print(e)

1 个答案:

答案 0 :(得分:0)

根据 addCategory(category){ let storageRef = firebase.storage().ref(`category/${category.img_name}`); //Upload file storageRef.put(category.url) .then((snapshot) => { // Here i will get download url var downloadUrl = snapshot.downloadURL; category.category_icon = downloadUrl.toString(); }) //Problem occurs here return is executed before .then statement and my download url becomes undefined. return this.http.post('https://app_name.cloudfunctions.net/api/products',body,{headers:headers}) .map(res =>res.json()); } ,预期的重定向似乎不是well-formed

requests

输出:

import requests

response = requests.get('http://chollo.to/675za')
for resp in response.history:
    print(resp.status_code, resp.url)
print(response.url)
print(response.is_redirect)

这可能是 epartner doubleclick 的故意。对于这些类型的嵌套URL,您需要一个额外的步骤,如:

301 http://chollo.to/675za
http://web.epartner.es/click.asp?ref=754218&site=14010&type=text&tnb=39&diurl=https%3A%2F%2Fad.doubleclick.net%2Fddm%2Fclk%2F302111021%3B129203261%3By%3Fhttp%3A%2F%2Fwww.elcorteingles.es%2Flimite-48-horas%2Fequipaje%2F%3Fsorting%3DpriceAsc%26aff_id%3D2118094
False

注意:通过这样做,您可能会避免预期的广告收入。