Python從URL中提取域名
問題描述
Python如何從URL中提取域名?url有各種格式的如下:
輸入:
https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1https://stackoverflow.com/questions/1234567/blah-blah-blah-blahhttp://www.domain.comhttps://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah ...
輸出:
docs.google.comstackoverflow.comwww.domain.comwww.other-domain.com
問題解答
回答1:使用Python 內置的模塊 urlparse
from urlparse import *url = ’https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1’result = urlparse(url)
result 包含了URL的所有信息
回答2:原文出處: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
相關文章:
1. javascript - ionic1的插件如何遷移到ionic2的項目中2. java - 如何在Fragment中調用Activity的onNewIntent?3. javascript - h5上的手機號默認沒有識別4. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat5. css - 關于input標簽disabled問題6. python - 獲取到的數據生成新的mysql表7. 怎么用css截取字符?8. window下mysql中文亂碼怎么解決??9. javascript - jquery hide()方法無效10. python的文件讀寫問題?
