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
相關(guān)文章:
1. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?2. debian - docker依賴的aufs-tools源碼哪里可以找到啊?3. 前端 - ng-view不能加載進模板4. 關(guān)docker hub上有些鏡像的tag被標記““This image has vulnerabilities””5. python - from ..xxxx import xxxx到底是什么意思呢?6. angular.js - angularJS在Android WebView中無法正常調(diào)后臺接口7. nignx - docker內(nèi)nginx 80端口被占用8. docker容器呢SSH為什么連不通呢?9. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應(yīng)10. ddos - apache日志很多其它網(wǎng)址,什么情況?
