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宿主機(jī)無法訪問容器中的服務(wù)?2. node.js - Cannot find module ’webpack/bin/config-yargs’3. thinkphp框架放根目錄無法像老師一樣可以正常訪問4. 樣式操作.css()5. $( "html" ).parent()方法返回一個包含document的集合,而$( "html" ).parents()返回一個空集合 哪位大神來解釋下?6. javascript - 動態(tài)添加路由報錯7. python - pyqt多線程問題實在不是很明白。。求解。。8. css3 - 獲取translateX的值9. css3 - CSSComb for Sublime Text 在 Windows 下無法使用。10. html5 - vue.js中,如何對轉(zhuǎn)義字符進(jìn)行表達(dá)式判斷?
