22

http://www.techcrunch.com/などのリンクがあり、リンクの techcrunch.com 部分だけを取得したいと考えています。Pythonでこれを行うにはどうすればよいですか?

4

9 に答える 9

31

urlparseを使用すると、ホスト名を簡単に取得できます。

hostname = urlparse.urlparse("http://www.techcrunch.com/").hostname

ただし、「ルート ドメイン」を取得することは、構文的な意味で定義されていないため、より問題になります。「www.theregister.co.uk」のルート ドメインは何ですか? デフォルト ドメインを使用するネットワークはどうですか? 「devbox12」は有効なホスト名である可能性があります。

これを処理する 1 つの方法は、パブリック サフィックス リストを使用することです。これは、実際のトップ レベル ドメイン (「.com」、「.net」、「.org」など) と、TLD のように使用されるプライベート ドメインの両方をカタログ化しようとします。 (例: ".co.uk" または ".github.io")。publicsuffix2ライブラリを使用して、Python から PSL にアクセスできます。

import publicsuffix
import urlparse

def get_base_domain(url):
    # This causes an HTTP request; if your script is running more than,
    # say, once a day, you'd want to cache it yourself.  Make sure you
    # update frequently, though!
    psl = publicsuffix.fetch()

    hostname = urlparse.urlparse(url).hostname

    return publicsuffix.get_public_suffix(hostname, psl)
于 2009-10-05T18:35:45.013 に答える
14

URL の一般的な構造:

scheme://netloc/path;parameters?query#fragment

TIMTOWTDIのモットー として:

urlparseを使用して、

>>> from urllib.parse import urlparse  # python 3.x
>>> parsed_uri = urlparse('http://www.stackoverflow.com/questions/41899120/whatever')  # returns six components
>>> domain = '{uri.netloc}/'.format(uri=parsed_uri)
>>> result = domain.replace('www.', '')  # as per your case
>>> print(result)
'stackoverflow.com/'  

tldextractを使用して、

>>> import tldextract  # The module looks up TLDs in the Public Suffix List, mantained by Mozilla volunteers
>>> tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')

あなたの場合:

>>> extracted = tldextract.extract('http://www.techcrunch.com/')
>>> '{}.{}'.format(extracted.domain, extracted.suffix)
'techcrunch.com'

tldextract一方、すべての gTLD [ジェネリック トップレベル ドメイン] と ccTLD [国コード トップレベル ドメイン] がどのように見えるかは、パブリック サフィックス リストに従って現在有効なものを検索することでわかります。したがって、URL を指定すると、ドメインからサブドメインを認識し、国コードからドメインを認識します。

チェリオ!:)

于 2017-01-29T10:37:09.410 に答える
0

______2.x ではなく Python 3.3 を使用する________

ベン・ブランクの答えに小さなことを追加したいと思います。

from urllib.parse import quote,unquote,urlparse
u=unquote(u) #u= URL e.g. http://twitter.co.uk/hello/there
g=urlparse(u)
u=g.netloc

今では、urlparseからドメイン名を取得しました。

サブドメインを削除するには、まず、どれがトップ レベル ドメインでどれがそうでないかを知る必要があります。たとえば、上記のhttp://twitter.co.uk-co.ukは TLD ですが、TLDとしてhttp://sub.twitter.comのみあり、サブドメインです。.comsub

したがって、すべてのtldsを含むファイル/リストを取得する必要があります。

tlds = load_file("tlds.txt") #tlds holds the list of tlds

hostname = u.split(".")
if len(hostname)>2:
    if hostname[-2].upper() in tlds:
        hostname=".".join(hostname[-3:])
    else:
        hostname=".".join(hostname[-2:])
else:
    hostname=".".join(hostname[-2:])
于 2015-08-21T18:19:11.080 に答える
0

パッケージや、これを行うために人々が提案している複雑さは必要ありません。以下のように簡単で、好みに合わせて調整できます。

def is_root(url):
    head, sep, tail = url.partition('//')
    is_root_domain = tail.split('/', 1)[0] if '/' in tail else url
    # printing or returning is_root_domain will give you what you seek
    print(is_root_domain)

is_root('http://www.techcrunch.com/')
于 2020-11-04T13:29:37.793 に答える
-4

これは私の目的のために働いた。シェアしようと思いました。

".".join("www.sun.google.com".split(".")[-2:])
于 2010-07-30T06:53:24.957 に答える