一尘不染

从URL获取协议+主机名

django

在我的Django应用中,我需要从引荐来源网址中获取主机名request.META.get('HTTP_REFERER')及其协议,以便从类似以下网址的网址中获取:

https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1
https://stackoverflow.com/questions/1234567/blah-blah-blah-blah
http://www.example.com
https://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah

我应该得到:

https://docs.google.com/
https://stackoverflow.com/
http://www.example.com
https://www.other-domain.com/

我查看了其他相关问题,并找到了有关urlparse的信息,但这并没有解决问题,因为

>>> urlparse(request.META.get('HTTP_REFERER')).hostname
'docs.google.com'

阅读 719

收藏
2020-03-26

共1个答案

一尘不染

你应该能够做到urlparse(docs:python2,python3):

from urllib.parse import urlparse
# from urlparse import urlparse  # Python 2
parsed_uri = urlparse('http://stackoverflow.com/questions/1234567/blah-blah-blah-blah' )
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)

# gives
'http://stackoverflow.com/'
2020-03-26