我想打印此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)
看起来不错。该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
>>>