Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Short link URL
#5
(Jul-09-2020, 02:41 PM)Evil_Patrick Wrote: Can you explain what's happening here?
URL shortening work bye redirect to the web page that has the original long URL.
When using allow_redirects=True it will follow all redirects.
The info info will be in the Location header.
To see more whats going on.
>>> import requests
>>> 
>>> url = 'http://t.co/hAplNMmSTg'
>>> session = requests.Session()
>>> resp = session.head(url, allow_redirects=True)
>>> resp.history
[<Response [301]>,
 <Response [301]>,
 <Response [301]>,
 <Response [301]>,
 <Response [301]>]
>>> 
>>> # We see that it gets redirect 5 times
>>> # Look at content of headers
>>> resp.history[0].headers
{'cache-control': 'no-cache, no-store, max-age=0', 'content-length': '0', 'date': 'Thu, 09 Jul 2020 16:50:07 GMT', 'location': 'https://t.co/hAplNMmSTg', 'server': 'tsa_o', 'x-connection-hash': '1a5db93459d4a04a1c4bef977f5ccbe5', 'x-response-time': '107'}
>>> resp.history[1].headers
{'cache-control': 'private,max-age=300', 'content-length': '0', 'date': 'Thu, 09 Jul 2020 16:50:07 GMT', 'expires': 'Thu, 09 Jul 2020 16:55:07 GMT', 'location': 'https://bit.ly/1kb2qbf', 'server': 'tsa_o', 'set-cookie': 'muc=1d5424b7-9c14-4a7f-a83b-093aed6c273f; Max-Age=63072000; Expires=Sat, 9 Jul 2022 16:50:07 GMT; Domain=t.co; Secure; SameSite=None', 'strict-transport-security': 'max-age=0', 'vary': 'Origin', 'x-connection-hash': '419be23ac2d73c03cafff5391745329d', 'x-response-time': '109'}
>>> resp.history[3].headers
{'Server': 'CloudFront', 'Date': 'Thu, 09 Jul 2020 16:50:08 GMT', 'Content-Type': 'text/html', 'Content-Length': '183', 'Connection': 'keep-alive', 'Location': 'https://www.wtatennis.com/players/player/13516/title/simona-halep', 'X-Cache': 'Redirect from cloudfront', 'Via': '1.1 8ddb6d7670d8c5a85c04a10525a71b91.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'OSL50-C1', 'X-Amz-Cf-Id': 'tqFBbmJSSfqO5YtsV_vJih8avgaWFdHf1NIQEGLAc-BlgquOqHrmCg=='}
>>> resp.history[4].headers
{'Content-Length': '0', 'Connection': 'keep-alive', 'Date': 'Thu, 09 Jul 2020 16:50:08 GMT', 'Location': '/players/314320/simona-halep', 'Server': 'nginx', 'X-Cache': 'Miss from cloudfront', 'Via': '1.1 1d8cf7c8865ed1078c19a98771ad34cb.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'OSL50-C1', 'X-Amz-Cf-Id': 'SnGpLonXzNkOnw2LpqTWdHp_I-3h4YkeYSQFr5WHjeyG7Dfy2mlPdw=='}
>>> resp.history[4].headers['Location']
'/players/314320/simona-halep'
So if run with this short url the end url will be.
Output:
https://www.wtatennis.com/players/314320/simona-halep
Reply


Messages In This Thread
Short link URL - by Evil_Patrick - Jul-09-2020, 09:56 AM
RE: Short link URL - by nuffink - Jul-09-2020, 02:13 PM
RE: Short link URL - by snippsat - Jul-09-2020, 02:14 PM
RE: Short link URL - by Evil_Patrick - Jul-09-2020, 02:41 PM
RE: Short link URL - by snippsat - Jul-09-2020, 05:02 PM
RE: Short link URL - by steve_shambles - Jul-10-2020, 10:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python how to check short url working? Pyguys 10 4,758 Mar-18-2020, 01:42 AM
Last Post: Pyguys
  get link and link text from table metulburr 5 6,353 Jun-13-2019, 07:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020