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. 求大神支招,php怎么操作在一個html文件的<head>標記內添加內容?2. 安裝了“PHP工具箱”,但只能以“游客”身份登錄3. 老師們php,插入數據庫mysql,都是空的,要怎么解決4. 跨類調用后,找不到方法5. 在mybatis使用mysql的ON DUPLICATE KEY UPDATE語法實現存在即更新應該使用哪個標簽?6. 致命錯誤: Class ’appfacadeTest’ not found7. 在應用配置文件 app.php 中找不到’route_check_cache’配置項8. PHP類屬性聲明?9. 怎么php怎么通過數組顯示sql查詢結果呢,查詢結果有多條,如圖。10. phpstady在win10上運行
