###前提・実現したいこと
BeautifulSoupでオライリー社の本の表紙をまとめてダウンロードしたい。
Windowsにアナコンダをインストールして使っています。
###発生している問題・エラーメッセージ
ページをダウンロード中http://www.oreilly.com/animals.csp...
画像をダウンロード中http://covers.oreilly.com/images/9780596155452/cat.gif...
ページをダウンロード中http://www.oreilly.com/animals.csp...
表紙画像が見つかりませんでした。
ページをダウンロード中http://www.oreilly.com/animals.csp...
表紙画像が見つかりませんでした。
と表示され続け、画像が最初の1枚のみしか保存されない。
###該当のソースコード
Python
1#! python3 2# downloadoreilly.py 3 4from urllib.request import urlopen 5import requests,os,bs4 6 7url='http://www.oreilly.com/animals.csp' #開始URL 8resp=urlopen(url) 9os.makedirs('oreilly',exist_ok=True) # ./oreillyに保存する 10 11while not url.endswith('1000'): 12 13 #ページをダウンロードする 14 print('ページをダウンロード中{}...'.format(url)) 15 res=requests.get(url) 16 res.raise_for_status() 17 18 soup=bs4.BeautifulSoup(resp.read(), 'html.parser') 19 20 #表紙画像のURLを見つける 21 oreilly_elem=soup.select('.animal-row img') 22 if oreilly_elem==[]: 23 print('表紙画像が見つかりませんでした。') 24 else: 25 oreilly_url=oreilly_elem[0].get('src') 26 #画像をダウンロードする 27 print('画像をダウンロード中{}...'.format(oreilly_url)) 28 res=requests.get(oreilly_url) 29 res.raise_for_status() 30 31 #画像を./oreillyに保存する 32 image_file=open(os.path.join('oreilly',os.path.basename(oreilly_url)),'wb') 33 for chunk in res.iter_content(1000): 34 image_file.write(chunk) 35 image_file.close() 36 37 38 39print('完了') 40
###補足情報(言語/FW/ツール等のバージョンなど)
http://www.oreilly.com/animals.cspを参照して表紙画像を全てダウンロードしたいと考えています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/10 09:38
2017/09/10 10:01