python爬取许多图片的代码
Posted 萧白白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬取许多图片的代码相关的知识,希望对你有一定的参考价值。
from bs4 import BeautifulSoup import requests import os os.makedirs(‘./img/‘, exist_ok=True) URL = "http://www.nationalgeographic.com.cn/animals/" html = requests.get(URL).text soup = BeautifulSoup(html, ‘lxml‘) img_ul = soup.find_all(‘ul‘, {"class": "img_list"}) for ul in img_ul: imgs = ul.find_all(‘img‘) for img in imgs: url = img[‘src‘] r = requests.get(url, stream=True) image_name = url.split(‘/‘)[-1] with open(‘./img/%s‘ % image_name, ‘wb‘) as f: for chunk in r.iter_content(chunk_size=128): f.write(chunk) print(‘Saved %s‘ % image_name)
以上是关于python爬取许多图片的代码的主要内容,如果未能解决你的问题,请参考以下文章