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. javascript - js 面向对象2. javascript - vuex 参数绑定问题求解3. javascript - Vue 自定义控件v-model双向绑定4. javascript - 关于js原生事件的绑定与解除绑定5. mysql group by多个字段6. python - requests post问题7. mysql插入文本如果是个sql语句就报错了8. node.js - 我是一个做前端的,求教如何学习vue,node等js引擎?9. require后不用使用echo返回到微信服务器 吗10. javascript - nodejs报错 打开localhost:3000 就报错

网公网安备