如何获取重定向的 URL? [英] How do I obtain redirected URLs?

查看:53
本文介绍了如何获取重定向的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 https://trade 的重定向 URL.ec.europa.eu/doclib/html/153814.htm 导致(pdf 文件).

I am trying to get the redirected URL that https://trade.ec.europa.eu/doclib/html/153814.htm leads to (a pdf file).

到目前为止我已经尝试过

I've so far tried

r = requests.get('https://trade.ec.europa.eu/doclib/html/153814.htm', allow_redirects = True)
print(r.url) 

并输出相同的旧 URL.我需要重定向的 URL,它是 https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf

and it outputs the same old URL. I need the redirected URL which is https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf

推荐答案

我认为当您输入 https://trade.ec.europa.eu/doclib/html/153814.htm 它给出您带有重定向链接的 HTML 页面,例如您可以像这样提取它

I think you should get a redirect link yourself (didn't found any way to do this with redirect), when you enter https://trade.ec.europa.eu/doclib/html/153814.htm it gives you HTML page with a redirect link, as for example you can extract it like this

import requests
from lxml import etree, html

tree = html.fromstring(requests.get('https://trade.ec.europa.eu/doclib/html/153814.htm').text)
print(tree.xpath('.//a/@href')[0])

输出将是

https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf

https://trade.ec.europa.eu/doclib/docs/2015/september/tradoc_153814.pdf

这篇关于如何获取重定向的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆