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 內(nèi)置的模塊 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. 如何解決docker宿主機無法訪問容器中的服務?2. angular.js - 輸入郵箱地址之后, 如何使其自動在末尾添加分號?3. javascript - 如何使用nodejs 將.html 文件轉化成canvas4. javascript - html5的data屬性怎么指定一個function函數(shù)呢?5. docker-compose中volumes的問題6. 在mac下出現(xiàn)了兩個docker環(huán)境7. python - Scrapy存在內(nèi)存泄漏的問題。8. javascript - 后臺管理系統(tǒng)左側折疊導航欄數(shù)據(jù)較多,怎么樣直接通過搜索去定位到具體某一個菜單項位置,并展開當前菜單9. angular.js - $stateChangeSuccess事件在狀態(tài)跳轉的時候不執(zhí)行?10. java如何生成token?
