get site name from a URL in python

前端 未结 3 1821
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 12:00

I am new to Python and it seems to have a lot of nice functions that I don\'t know about. What function can I use to get the root site name? For example, how would I get

相关标签:
3条回答
  • 2020-12-11 12:34

    The much overlooked urlparse module:

    from urlparse import urlparse
    scheme, netloc, path, params, query, fragment = urlparse("http://www.faqs.org/docs/diveintopython/kgp_commandline.html")
    print netloc
    
    0 讨论(0)
  • 2020-12-11 12:40
     >>> from urllib.parse import urlparse
     >>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html').hostname
     'www.cwi.nl'
    
    0 讨论(0)
  • 2020-12-11 12:42

    What version of Python are you learning with? Note that SilentGhost's answer is for Python 3.0, while Alabaster Codify's will work with the 2.x series.

    0 讨论(0)
提交回复
热议问题