Python从URL中提取域名

查看:448
本文介绍了Python从URL中提取域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

Python如何从URL中提取域名?url有各种格式的如下:

输入:

https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1
https://stackoverflow.com/questions/1234567/blah-blah-blah-blah
http://www.domain.com
https://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah ...

输出:

docs.google.com
stackoverflow.com
www.domain.com
www.other-domain.com

解决方案

使用Python 内置的模块 urlparse

from urlparse import *
url = 'https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1'
result = urlparse(url)

result 包含了URL的所有信息

这篇关于Python从URL中提取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆