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. MySQL中无法修改字段名的疑问2. angular.js - angularjs的自定义过滤器如何给文字加颜色?3. docker镜像push报错4. angular.js - angular内容过长展开收起效果5. javascript - 如何让移动端网页的输入框固定在底部?6. 请教各位大佬,浏览器点 提交实例为什么没有反应7. python的前景到底有大?如果不考虑数据挖掘,机器学习这块?8. python - flask表单 如何把提交多行数据在服务端读取出来?9. 大家好,请问在python脚本中怎么用virtualenv激活指定的环境?10. 网页爬虫 - 用Python3的requests库模拟登陆Bilibili总是提示验证码错误怎么办?
