Python第二个项目:爬取一个网站的所有图片

前面搭建爬虫环境完成第一个实例爬取信息,今天继续,Python第二个项目:爬取一个网站的所有图片,看到一个网站的图片,想要弄到本地,一张张保存太过于麻烦,于是,往下看:

爬取目标网站:https://www.yangqq.com/skin/jxhx/

1:确保已经安装 requests模块如果没有安装,可以使用命令安装,安装命令如下:

pip install requests

成功安装模块requests

2:新建一个first.py 文件,写代码如下

#_*_coding:utf-8_*_
import requests
import re
import os
class GetImage(object):
    def __init__(self,url):
        self.url = url
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
        }
        self.dir_path = os.path.dirname(os.path.abspath(__file__))
        self.path = self.dir_path+'/imgs'
        isExists = os.path.exists(self.dir_path+'/imgs')
        # 创建目录
        if not isExists:
            os.makedirs(self.path)

    def download(self,url):
        try:
            res = requests.get(url,headers=self.headers)
            return res
        except Exception as E:
            print(url+'下载失败,原因:'+E)


    def parse(self,res):
        content = res.content.decode()
        # print(content)
        img_list = re.findall(r'<img.*?src="(.*?)"',content,re.S)
        img_list = ['http://www.yangqq.com/skin/jxhx/'+url for url in img_list]
        return img_list

    def save(self,res_img,file_name):
        if res_img:
            with open(file_name,'wb') as f:
                f.write(res_img.content)
            print(url+'下载成功')

    def run(self):
        # 下载
        res = self.download(self.url)
        # 解析
        url_list = self.parse(res)
        # 下载图片
        for url in url_list:
            res_img = self.download(url)
            name = url.strip().split('/').pop()
            file_name = self.path+'/'+name
            # 保存
            self.save(res_img,file_name)

if __name__ == '__main__':
    url_list = ['https://www.yangqq.com/skin/jxhx/', 'https://www.yangqq.com/skin/jxhx/list.html',
                'https://www.yangqq.com/skin/jxhx/share.html', 'https://www.yangqq.com/skin/jxhx/list2.html',
                'https://www.yangqq.com/skin/jxhx/list3.html', 'https://www.yangqq.com/skin/jxhx/daohang.html',
                'https://www.yangqq.com/skin/jxhx/about.html']
    for url in url_list:
        text = GetImage(url)
        text.run()

3:在文件上鼠标右键点击运行按钮,得到结果如下

4:可以看到生成的imgs文件夹里面,想要的图片都已经下载在文件夹里面了

ok,完成

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券