使用 Python 从 URL 获取子域 [英] Get subdomain from URL using Python

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

问题描述

例如地址为:

地址 = http://lol1.domain.com:8888/some/page

我想将子域保存到一个变量中,这样我就可以这样做;

I want to save the subdomain into a variable so i could do like so;

print SubAddr
>> lol1

推荐答案

urlparse.urlparse 将 URL 拆分为协议、位置、端口等,然后可以通过 拆分位置. 获取子域.

urlparse.urlparse will split the URL into protocol, location, port, etc. You can then split the location by . to get the subdomain.

url = urlparse.urlparse(address)
subdomain = url.hostname.split('.')[0]

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

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