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类中的$this2. javascript - 请教如何获取百度贴吧新增的两个加密参数3. javascript - 使用百度文本编辑器ueditor不显示样式问题4. python - 用__call__ 实现装饰器功能5. html5 - canvas有时候会拿不到toDataURL数据6. android - 安卓实现类似QQ刚换聊天背景的功能7. javascript - 微信小程序 wx.downloadFile下载文件大小有限制吗8. 微信小程序9. 输入地址报以下截图错误,怎么办?10. css - html文件上传到虚拟主机后使用谷歌浏览器打开html元素的宽度变大
