获取一个网址http://www.veryyx.com/......中的www.veryyx.com

本文介绍了Python内置的urllib.parse模块中的urlparse函数,用于解析URL并将其拆分为六个组件:scheme、netloc、path、params、query和fragment。通过示例代码展示了如何获取URL的netloc部分,例如从'https://www.baidu.com/?tn=98012088_5_dg&ch=12'中提取出'www.baidu.com'。该函数对于处理和分析URL非常有用。

可以使用python中的内置模块urllib.parse中的urlparse

以下为urlparse说明

Help on function urlparse in module urllib.parse:

urlparse(url, scheme='', allow_fragments=True)
    Parse a URL into 6 components:
    <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
    Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
    Note that we don't break the components up in smaller bits

    (e.g. netloc is a single string) and we don't expand % escapes.

urlparse将其解析为6个部分

获取www.baidu.com只需要将<scheme>://<netloc>/<path>;<params>?<query>#<fragment>

中netloc部分取出即可

netloc只会取到://到第一个/之间的字符串

代码演示:

import urllib.parse
 
url = 'https://www.baidu.com/?tn=98012088_5_dg&ch=12'
sp = urllib.parse.urlparse(url)
 
print(sp.netloc)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值