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. 怎样用python给mp3添加封面?2. javascript - 微信 H5 授权 返回键3. windows-server - 阿里云服务器给了权限,本地采集的数据远程写入服务器中MySQL5.5表中,一秒可以写入多少?这个速度一般由什么来决定?4. MongoDB的索引需要考虑每个键的索引方向,为何这么设计?而MySQL的索引则无需考虑方向,两者之间有何优劣势?5. js怎样实现在类似qq的联系人分组中进行搜索后,又可恢复原分组的界面?6. mysql优化 - mysql 一张表如果不能确保字段列长度一致,是不是就不需要用到char。7. python - scrapy-redis爬虫运行是连接redis数据库连接错误8. python - 如何将文本文件的CR换行符换成LF换行符?9. TP6里多应用里如何开启调试跟踪10. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。