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. mongodb - windows7下mongod无法正常启动2. 修改mysql配置文件的默认字符集重启后依然不生效3. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战4. angular.js - 关于指令link 中的创建变量问题5. Python两个list互换,两种不同的操作结果有差异,原因是什么呢?6. php - 类似Apple官网顶部3级导航该如何设计数据库?7. MySQL视图count速度优化8. python sqlite3 长语句插入出错9. javascript - ueditor引入报错问题10. 反应RTL有条件导入CSS

网公网安备