import time
import urllib
import os
import re
from bs4 import BeautifulSoup
import requests
# from urllib.request import urlretrieve
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, br',
'Cookie': 'gr_user_id=546172fa-e41a-435a-ae8b-03aa14a54b8b; acw_tc=2760821c16369652778958602e88299a0b9105bad593d33d8f74a59d6aafd2; JSESSIONID=2F2C2EAC9086E016E94F07278A3C9CC1; gr_session_id_9311c428042bb76e=984df283-4b7b-4f25-a9d5-b9d5fb0ab693; gr_session_id_9311c428042bb76e_984df283-4b7b-4f25-a9d5-b9d5fb0ab693=true'
}
def mkdir(path):
'''
创建指定的文件夹
:param path: 文件夹路径,字符串格式
:return: True(新建成功) or False(文件夹已存在,新建失败)
'''
# 引入模块
import os
# 去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists = os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' 目录已存在')
return False
def reqAndDownload1():
url = "https://www.vcg.com/sets/ziranfengguang-1/"
# 发送get请求,请求到的数据字典
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.select('.big')
folder = './视觉中国/自然风光/'
mkdir(folder)
for index, item in enumerate(title):
style = item.attrs['style']
pt = r"url\('(.*)'\)"
opt = re.findall(pt, style)
imgUrl = 'https:' + opt[0]
imgName = os.path.basename(imgUrl)
print(imgName, '下载中...', sep='\t', end='\t', flush=True)
r = requests.get(imgUrl, headers=headers)
with open(folder+imgName, 'wb') as f:
f.write(r.content)
print('下载完成')
def reqAndDownload2():
url = "https://www.vcg.com/sets/522061344"
# 发送get请求,请求到的数据字典
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.select('.imgWaper img')
folder = './视觉中国/2022二季度创意图片——热门TOP100/'
mkdir(folder)
for index, item in enumerate(title):
src = item['src']
imgUrl = 'https:' + src
imgName = os.path.basename(imgUrl)
print(imgName, '下载中...', sep='\t', end='\t', flush=True)
r = requests.get(imgUrl, headers=headers)
with open(folder+imgName, 'wb') as f:
f.write(r.content)
print('下载完成')
def reqAndDownload3():
print('下载中...', end='\t')
img_url = 'https://alifei01.cfp.cn/cms/image/image/54965dde906d4542a8a806434fac174e.jpg?x-oss-process=image/format,webp'
urllib.request.urlretrieve(img_url, '1.jpg')
print('下载完成')
# reqAndDownload1()
reqAndDownload2()
视觉中国的图片下载
于 2022-07-19 11:08:14 首次发布