如何从URL解析最终域名

问题描述 投票:0回答:1

我有一个怪异的Facebook URL“ https://l.facebook.com/l.php?u=http%3A%2F%2Fon.theatln.tc%2FEQs2HvJ%3Ffbclid%3DIwAR3TDwS8n30YYzCVqPceAVd3D_OYmy_SHWGTqKjNVPaI_C0qZn9OgIMRdAQ&h=AT2IGmpY8Hn4DC7nr9l8PScyiYSQYxxP9mY9cA0z09dgbhN0s4nv6lXhrxXDtRVkfwDbRqb5jMMiK_XIVkUooNdUlNA1v56my_FaYOPy7CskvfoqH5VrrICUs7qoq83L_TWzmm8SE4pp8v1l0ikDBvky35KSq3FCGTLhgkTm3XP4XNXCur-R80JJwwmWtLspm5tbpx26475YU1yUdognJg2CCCredWJehAth4xsqeOFUw40EvwWtBrtRgDZdE3ERHr7_53dccR6XnZqR5Jwqdh-UkVkY-UdItvLicSwmjWT7IErBGTmBDoFx0pQk4Hc4SvoJ-CIZkRwOBUHA41-OzQIvlSqbbWdCZvvs2b9b3PO87ZFlh07XKnj7Lp4EJD_SPl-10yDvEZOaLfz23Mljr2aVTcnuDw9mWqWtb_bY0LLiCKJlmUK6CU2fzfJre6aN1AfOncKnTLVa6Kf1-cB-F5ftF8icUWye1hBmJ5LQCxbqlq1AAyonYk2L-Kiw6nytcu5g-MYCYhy8pAuOVn_YioWY0wa93xjlySV45GkMW3H0xtv_tRVnPsGo-Ts0CpshD1Xx9B9txOXP-WvipQ9AXV2uBN33WXq01xULSRr3bN3qGKsR7wo3LOl8w0QwyMDH4-0

我想打印此URL指向的链接,即“ https://www.theatlantic.com/culture/archive/2020/05/upload-black-mirror-hell-digital-heaven/611293/

我的代码可以解析大多数Facebook URL,但是我最近发现它不适用于这种类型。打印“ http://on.theatln.tc/EQs2HvJ。”

# url is the long FB url

            import urllib.parse as url_parse
            news_link = url_parse.unquote(url).split("?u=")[1].split("?fbclid")[0]
            print(news_link)
python url
1个回答
0
投票

看起来不错。该URL(http://on.theatln.tc/EQs2HvJ)似乎是嵌入在Facebook URL中的链接。如果获取该链接,则可能会解析为到最终目标的重定向。考虑使用请求模块来遵循重定向:

>>> import requests
>>> x = requests.get('http://on.theatln.tc/EQs2HvJ')
>>> print(x.url)
https://www.theatlantic.com/culture/archive/2020/05/upload-black-mirror-hell-digital-heaven/611293/?utm_source=facebook&utm_campaign=the-atlantic&utm_content=edit-promo&utm_medium=social&utm_term=2020-05-07T16%3A56%3A03
>>>
© www.soinside.com 2019 - 2024. All rights reserved.