0
0

More than 3 years have passed since last update.

音声ファイルをスクレイピングで一括ダウンロードする【Python】【urllib】

Last updated at Posted at 2020-09-23

概要

こちらのページからポケモンの鳴き声のwavファイルをプログラミングでダウンロード(スクレイピング)するコードの紹介です。音声ファイルのダウンロードにurllib.request.urlretrieveという関数を使うのがポイントです。

スクレイピングはマナー、法律を守って自己責任で実行してください。

環境

macOS Catalina 10.15.4
python 3.8.0

ライブラリのインストール

pip install beautifulsoup4
pip install requests
pip install urllib

コード

from bs4 import BeautifulSoup
import requests
import urllib
import time

base_url = 'http://games255.512.jp/'
index_page_url = base_url+'pokewav_DL/index.html'
html_text = requests.get(index_page_url).content #文字化け対策に.textではなく.contentを使う
soup = BeautifulSoup(html_text,"html.parser")

a_tags = soup.find_all('a')
for a in a_tags:
    path = a.get('href')
    if 'pokewav' not in path: continue
    abs_path = base_url + '/'.join(path.split('/')[1:])
    wav_file_name = a.text.replace(':','_')+'.wav'
    urllib.request.urlretrieve(abs_path,wav_file_name)
    print(wav_file_name,'is downloaded')
    time.sleep(1)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0