BeautifulSoup の href 取得 [重複] 質問する

BeautifulSoup の href 取得 [重複] 質問する

私は次のものを持っていますsoup:

<a href="some_url">next</a>
<span class="class">...</span>

ここからhrefを抽出したいのですが、"some_url"

タグが 1 つしかない場合は実行できますが、ここではタグが 2 つあります。テキストも取得できます'next'が、それは私が望んでいるものではありません。

また、APIのわかりやすい説明と例がどこかにありますか。私は標準ドキュメントですが、もう少し整理されたものを探しています。

ベストアンサー1

find_all次のように使用して、属性aを持つすべての要素を検索しhref、それぞれを印刷できます。

# Python2
from BeautifulSoup import BeautifulSoup
    
html = '''<a href="some_url">next</a>
<span class="class"><a href="another_url">later</a></span>'''
    
soup = BeautifulSoup(html)
    
for a in soup.find_all('a', href=True):
    print "Found the URL:", a['href']

# The output would be:
# Found the URL: some_url
# Found the URL: another_url
# Python3
from bs4 import BeautifulSoup

html = '''<a href="https://some_url.com">next</a>
<span class="class">
<a href="https://some_other_url.com">another_url</a></span>'''

soup = BeautifulSoup(html)

for a in soup.find_all('a', href=True):
    print("Found the URL:", a['href'])

# The output would be:
# Found the URL: https://some_url.com
# Found the URL: https://some_other_url.com

古いバージョンのBeautifulSoup(バージョン4以前)を使用している場合、このメソッドの名前は であることに注意してくださいfindAll。バージョン4では、BeautifulSoupのメソッド名はPEP 8に準拠するように変更されましたなので、find_all代わりに を使用する必要があります。


すべてのタグが必要な場合はhref、パラメータを省略できますname

href_tags = soup.find_all(href=True)

おすすめ記事