python相册代码_分享个 Python 图片下载代码

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import requests

import re

import os

def getHTMLText(url):

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"}

try:

r = requests.get(url,headers=headers)

r.raise_for_status()

return r.text

except requests.exceptions.RequestException as e:

print(e)

def getURLList(html):

regex = r"( http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png)"

lst = []

matches = re.finditer(regex, html, re.MULTILINE)

for x,y in enumerate(matches):

try:

lst.append(str(y.group()))

except:

continue

return sorted(set(lst),key = lst.index)

def download(lst,filepath='img'):

if not os.path.isdir(filepath):

os.makedirs(filepath)

filecounter = len(lst)

filenow = 1

for url in lst:

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"}

filename = filepath +'/' + url.split('/')[-1]

with open(filename,'wb') as f :

try:

img = requests.get(url,headers=headers)

img.raise_for_status()

print("Downloading {}/{} file name:{}".format(filenow,filecounter,filename.split('/')[-1]))

filenow += 1

f.write(img.content)

f.flush()

f.close()

print("{} saved".format(filename))

except requests.exceptions.RequestException as e:

print(e)

continue

if __name__ == '__main__':

url = input('please input the image url:')

filepath = input('please input the download path:')

html = getHTMLText(url)

lst = getURLList(html)

download(lst,filepath)

需要 requests 库

运行效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值