【问题标题】:How to get host name from website using python如何使用python从网站获取主机名
【发布时间】:2021-02-05 20:36:35
【问题描述】:

我想查看在 python 中使用请求存储库获取主机名。我试着这样做:

pprint(requests.get("https://www.facebook.com/").headers['domain'])

但它不起作用。如果有可能在其他存储库中,我将不胜感激。

【问题讨论】:

标签: python


【解决方案1】:

基于Regular Expression - Extract subdomain & domain

import requests
import re

p = re.compile("^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/?\n]+)")

r = requests.get("https://www.google.com/")
domain = p.match(r.url).group(1)
print(domain)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 2021-11-11
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多