けむブログ

データ分析に関する気付きや学びを記録するブログ

python スクレイピング ~タグの中のテキストと、リンクを取得する時~

スクレイピングして、タグの中身を取得する時の方法

テキストのみ取得する時

・findAll(text = True) で取得できる!

for news in contents:
    for anchor_text in news.findAll('a'):
        print(anchor_text.findAll(text=True)) 
#findAll(text=True) でテキストのみを抽出できる
リンクを取得する時

・.get('href') で取得できる!

for news in contents:
    for anchor_text in news.findAll('a'):
        print(anchor_text.get('href')) #リンクのみを取得できる