码迷,mamicode.com
首页 > 编程语言 > 详细

python爬取许多图片的代码

时间:2018-12-03 21:44:05      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:ogr   sts   nim   makedirs   hunk   graphic   import   col   img   

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爬取许多图片的代码

标签:ogr   sts   nim   makedirs   hunk   graphic   import   col   img   

原文地址:https://www.cnblogs.com/jianglijian/p/10060960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!