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實(shí)用腳本清單
從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 - 各位電腦上有多少個(gè)容器啊?容器一多,自己都搞混了,咋辦呢?2. MySQL數(shù)據(jù)庫中文亂碼的原因3. macos - mac下docker如何設(shè)置代理4. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””5. docker不顯示端口映射呢?6. java - 請(qǐng)問在main方法中寫成對(duì)象名.屬性()并賦值,與直接參參數(shù)賦值輸錯(cuò)誤是什么原因?7. css - C#與java開發(fā)Windows程序哪個(gè)好?8. docker gitlab 如何git clone?9. android studio總是在processes running好久10. docker-compose 為何找不到配置文件?
