隅歩つ

書いて理解を深める

PythonでWebページを取得する

スクレイピングができるようになりたい。

なぜなら、私はスクレイピングがかっこいいと思っているからです。

広大なネットの海から必要な情報を探し出すことはかっこいいです。

スクレイピングはその第1歩だと思ってます。

Webページを取得する方法

まずは、requestsをインポートします。

import requests

次に、ページURLを設定します。
今回はグーグル(日本)にしました。

url = 'https://www.google.co.jp/'

requestsでURLを取得します。
「response.apparent_encoding」で文字化けを防ぎます。

response = requests.get(url)
response.encoding = response.apparent_encoding

ダウンロードする先のtxtファイルを設定します。

filename = 'google_co_jp.txt'

最後に、txtファイルに書き込みます。

with open(filename, mode="w") as f:
    f.write(response.text)

コード全体

コード全体は↓です。

import requests

url = 'https://www.google.co.jp/'

response = requests.get(url)
response.encoding = response.apparent_encoding

filename = 'google_co_jp.txt'

with open(filename, mode="w") as f:
    f.write(response.text)

yuuuha.hatenablog.com