from urllib.parse import urlparse result = urlparse("http://sports.sohu.com/20041115/b222992554.shtml") print(result) url_lb = result.hostname.strip().split('.')[0] print(url_lb)
輸出結果爲:html
D:\installed\Anaconda3\python.exe E:/文本分類——3/delete.py ParseResult(scheme='http', netloc='sports.sohu.com', path='/20041115/b222992554.shtml', params='', query='', fragment='') sports Process finished with exit code 0