python循环抓取多张图片
测试抓取 国家地理中文网的小动物们
网址 http://www.ngchina.com.cn/animals/
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import os
html=requests.get('http://www.ngchina.com.cn/animals/').text
# html=html.decode('utf8')
# print(html.encoding)
# print(html.decode("UTF-8"))
soup=BeautifulSoup(html,features="html.parser")
img_ul=soup.find_all('ul',{'class':'img_list'})
os.makedirs('../img',exist_ok=True)
for ul in img_ul:
imgs=ul.find_all('img')
for img in imgs:
url=img['src']
r=requests.get(url)
image_name=url.split('/')[-1]
with open('../img/%s'%image_name,'wb') as f:
f.write(r.content)
print("saved %s"%image_name)