首页 » Python » Python爬虫---爬取图片

Python爬虫---爬取图片

原文 http://blog.csdn.net/qq_38251616/article/details/79230856

2018-02-02 02:01:07阅读(196)

# coding:UTF-8
import requests
import os
import time
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
if __name__ == '__main__':
    
    list_url = []
    url = 'https://www.zhihu.com/question/22918070'
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
    }
    req = requests.get(url=url,headers=headers)
    req.encoding='utf-8'
    html = req.text
    bf = BeautifulSoup(html,'html.parser')
    targets_url_1 = bf.find_all('figure')
    bf = BeautifulSoup(str(targets_url_1),'html.parser')
    targets_url = bf.find_all('noscript')
    
    #保存图片链接
    for each in targets_url:
        list_url.append(each.img.get('src'))
    
    for each_img in list_url:
        #准备下载图片
        if 'images6' not in os.listdir():
            os.makedirs('images6')   
        #下载图片
        urlretrieve(url=each_img,filename = 'images6/'+'%s.jpg' % time.time())
        time.sleep(1)
    
    print('下载完成!')

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹