Python如何從URL中提取網域名稱? url有各種格式的如下:
輸入:
https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1
https://stackoverflow.com/questions/1234567/blah-blah-blah-blah
http://www.domain.com
https://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah ...
輸出:
docs.google.com
stackoverflow.com
www.domain.com
www.other-domain.com
仅有的幸福2017-06-28 09:24:24
使用Python 內建的模組 urlparse
from urlparse import *
url = 'https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1'
result = urlparse(url)
result 包含了URL的所有資訊
ringa_lee2017-06-28 09:24:24
原文出處:Python實用腳本清單
從URL中提取網域
def extractDomainFromURL(url):
"""Get domain name from url"""
from urlparse import urlparse
parsed_uri = urlparse(url)
domain = '{uri.netloc}'.format(uri=parsed_uri)
return domain