Scamming shorteners with python

Asked

Viewed 137 times

1

I need for my project, some function using Python 2.7 enter a URL, and get the next URL from it. -> Example: goo.Gl/LAKFXG = / bit.ly/2D3SEnZ = /

I don’t want to use the API of these URLS, but something that really goes into the url and gives me the final URL. However, I’m out of ideas. Any library? Remembering that I don’t want to use the Shorteners API.

  • Since I don’t work with Python, I’ll leave a hint that might help you. Use the command cURL Python to access this url. However, instead of returning "body", you return only the "headers". Having the headers, simply filter by Location, followlocation, etc..

  • What you’re implementing is something like unshortener URL?

1 answer

4


import urllib2
r = urllib2.urlopen(url="https://shorturl.com")
print 'URL redirecionada:', r.url
  • urllib2.Httperror: HTTP Error 403: Forbidden What do I do in this case?

  • Which URL did you try? According to the status codes, this is a prohibition error: https://pt.wikipedia.org/wiki/Lista_de_code_state_HTTP#403_Proibido >>> r = urllib2.urlopen(url="https://goo.gl/LAKFXG")
>>> r.url
'https://answall.com/'

  • Not headers. Which URL did you try? So I can replicate the error.

  • 1

    Heey! I got rid of the bug. 403 Forbidden! Pastebin.com/5tyzikbD Thanks Frank!

  • Now man. I would strongly advise the use of the requests library and python3. If you do not have with some project in clear production environment.

  • Where requests + python3 would disrupt environments in production @Franklintimothy ?

  • He is using python2, what I meant is that he can switch to python3 and use the requests library, but if he is using python2 with project in production, do you agree that it would be difficult to make this exchange overnight? The point is: change to python3 as soon as possible, but if you already use python2 and do not want to waste time with rework, continue with python2 until you have time to make the appropriate modification.

  • The urllib2 documentation itself recommends the use of requests. https://docs.python.org/2/library/urllib2.html . Ahh but this is just to get a final URL, why use a more robust library? True, it doesn’t make sense. But if we think about the future where you wanted to improve your program?!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.