I.何をする?

次を参考にしてBeautifulsoupの使い方を学びます。 Super Simple Way to Scrape BBC News Articles in Python

II.仮想環境の作成

Iの文書に書かれています。

III.Beautifulsoupの基本

ライブラリをインストールし、BBCのトップページを読み込みます。

import requests
from bs4 import BeautifulSoup as bs
page = requests.get('https://www.bbc.com/news')
soup = bs(page.content, 'html.parser')

一つ目のヘッドライン(h1)を見つけ出すコードは次です。

h1 = soup.find('h1')
print(h1.get_text())
## BBC News Home

第3番目の文字列を取得します。

soup.find_all('p')[2].get_text()
## 'Benjamin Netanyahu says Israel will "continue to respond forcefully" to rocket attacks from Gaza.'

Not completed yet.