BeautifulSoup を使用して DOM ツリーを解析し、作成者の名前を抽出しようとしています。以下は、これからスクレイピングするコードの構造を示す HTML のスニペットです。
<html>
<body>
<div class="list-authors">
<span class="descriptor">Authors:</span>
<a href="/find/astro-ph/1/au:+Lin_D/0/1/0/all/0/1">Dacheng Lin</a>,
<a href="/find/astro-ph/1/au:+Remillard_R/0/1/0/all/0/1">Ronald A. Remillard</a>,
<a href="/find/astro-ph/1/au:+Homan_J/0/1/0/all/0/1">Jeroen Homan</a>
</div>
<div class="list-authors">
<span class="descriptor">Authors:</span>
<a href="/find/astro-ph/1/au:+Kosovichev_A/0/1/0/all/0/1">A.G. Kosovichev</a>
</div>
<!--There are many other div tags with this structure-->
</body>
</html>
私の混乱のポイントは、soup.find を実行すると、検索している div タグの最初の出現が検出されることです。その後、すべての 'a' リンク タグを検索します。この段階で、各リンク タグから著者名を抽出して出力するにはどうすればよいでしょうか。BeautifulSoup を使用してそれを行う方法はありますか、それとも正規表現を使用する必要がありますか? 他のすべての div タグを繰り返し処理し、著者名を抽出するにはどうすればよいですか?
import re
import urllib2,sys
from BeautifulSoup import BeautifulSoup, NavigableString
html = urllib2.urlopen(address).read()
soup = BeautifulSoup(html)
try:
authordiv = soup.find('div', attrs={'class': 'list-authors'})
links=tds.findAll('a')
for link in links:
print ''.join(link[0].contents)
#Iterate through entire page and print authors
except IOError:
print 'IO error'