美文网首页Python数据从入门到实践
Python爬虫入门—图片下载

Python爬虫入门—图片下载

作者: Sunflow007 | 来源:发表于2020-03-03 23:00 被阅读0次
2.jpg

既然开辟了Python专栏,当然少不了图片下载了,自己学Python写的第一个程序就是为了爬图片的,当时是找了个图片排列比较规则的网站糗事百科,然后当第一个程序完工时,看着一张张图片自己下载到电脑,自己命名打包成文件夹,那个激动啊,然后紧接着又写了代码,爬取美女图,嘿嘿你懂得~后来,就没怎么用过爬虫下载图片了。。。。看来还是需求产生动力,屁股决定脑袋!

这次决定写一个爬虫程序来爬精美的壁纸图,壁纸网站看了知乎网友的推荐,选择的是:

Awesome Wallpapers - wallhaven.cc​alpha.wallhaven.cc

image

此程序支持输入关键词,根据查询结果返回图片总数、然后在本地新建文件夹、字典下载图片到文件夹中。
效果如下:

image

代码如下:

#_*_ coding:utf-8 _*_
#__author__='阳光流淌007'
#__date__='2018-01-21'
#爬取wallhaven上的的图片,支持自定义搜索关键词,自动爬取并该关键词下所有图片并存入本地电脑。
import os
import requests
import time
import random
from lxml import etree

keyWord = input(f"{'Please input the keywords that you want to download :'}")
class Spider():
    #初始化参数
    def __init__(self):
        #headers是请求头,"User-Agent"、"Accept"等字段都是通过谷歌Chrome浏览器查找的!
        self.headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36",
        }
        #filePath是自定义的,本次程序运行后创建的文件夹路径,存放各种需要下载的对象。
        self.filePath = ('/users/zhaoluyang/小Python程序集合/桌面壁纸/'+ keyWord + '/')

    def creat_File(self):
        #新建本地的文件夹路径,用于存储网页、图片等数据!
        filePath = self.filePath
        if not os.path.exists(filePath):
            os.makedirs(filePath)

    def get_pageNum(self):
        #用来获取搜索关键词得到的结果总页面数,用totalPagenum记录。由于数字是夹在形如:1,985 Wallpapers found for “dog”的string中,
        #所以需要用个小函数,提取字符串中的数字保存到列表numlist中,再逐个拼接成完整数字。。。
        total = ""
        url = ("https://alpha.wallhaven.cc/search?q={}&categories=111&purity=100&sorting=relevance&order=desc").format(keyWord)
        html = requests.get(url)
        selector = etree.HTML(html.text)
        pageInfo = selector.xpath('//header[@class="listing-header"]/h1[1]/text()')
        string = str(pageInfo[0])
        numlist = list(filter(str.isdigit,string))
        for item in numlist:
            total += item
        totalPagenum = int(total)
        return totalPagenum

    def main_fuction(self):
        #count是总图片数,times是总页面数
        self.creat_File()
        count = self.get_pageNum()
        print("We have found:{} images!".format(count))
        times = int(count/24 + 1)
        j = 1
        for i in range(times):
            pic_Urls = self.getLinks(i+1)
            for item in pic_Urls:
                self.download(item,j)
                j += 1

    def getLinks(self,number):
        #此函数可以获取给定numvber的页面中所有图片的链接,用List形式返回
        url = ("https://alpha.wallhaven.cc/search?q={}&categories=111&purity=100&sorting=relevance&order=desc&page={}").format(keyWord,number)
        try:
            html = requests.get(url)
            selector = etree.HTML(html.text)
            pic_Linklist = selector.xpath('//a[@class="jsAnchor thumb-tags-toggle tagged"]/@href')
        except Exception as e:
            print(repr(e))
        return pic_Linklist

    def download(self,url,count):
        #此函数用于图片下载。其中参数url是形如:https://alpha.wallhaven.cc/wallpaper/616442/thumbTags的网址
        #616442是图片编号,我们需要用strip()得到此编号,然后构造html,html是图片的最终直接下载网址。
        string = url.strip('/thumbTags').strip('https://alpha.wallhaven.cc/wallpaper/')
        html = 'http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-' + string + '.jpg'
        pic_path = (self.filePath + keyWord + str(count) + '.jpg' )
        try:
            pic = requests.get(html,headers = self.headers)
            f = open(pic_path,'wb')
            f.write(pic.content)
            f.close()
            print("Image:{} has been downloaded!".format(count))
            time.sleep(random.uniform(0,2))
        except Exception as e:
            print(repr(e))

spider = Spider()
spider.main_fuction()

image image

注意:
图片虽好,可是下载速度挺慢的,可能和网站服务器有关没有验证过网站是否有反爬虫机制,加了time.sleep(random.uniform(0,2))让图片下载完后停顿0-2秒,1来防止给服务器带来过大压力,2来也怕被爬虫机制检测~

相关文章

网友评论

    本文标题:Python爬虫入门—图片下载

    本文链接:https://www.haomeiwen.com/subject/zgnllhtx.html